From 71e27ce9c36faba26637b4188fdb8bec496624d5 Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Sun, 26 Jul 2020 23:17:49 +0200 Subject: [PATCH] Fixes on new model --- app/DataTables/Shop/ArticlesDataTable.php | 19 +------ .../ArticleAttributeFamilyController.php | 2 +- app/Models/Shop/Article.php | 5 ++ app/Models/Shop/ArticlePrice.php | 19 ++++++- app/Models/Shop/Category.php | 3 +- app/Repositories/Shop/ArticleAttributes.php | 18 +++++-- app/Repositories/Shop/ArticlePrices.php | 13 +++-- app/Repositories/Shop/Articles.php | 8 ++- composer.json | 1 + .../Botanic/Admin/Families/list.blade.php | 2 +- .../ArticleAttributeFamilies/index.blade.php | 8 +-- .../ArticleAttributeFamilies/list.blade.php | 4 -- .../ArticleAttributeValues/index.blade.php | 49 +++++++++++++++++++ .../ArticleAttributeValues/list.blade.php | 2 +- .../Shop/Admin/ArticleFamilies/list.blade.php | 6 +-- .../views/Shop/Admin/Articles/edit.blade.php | 2 +- .../views/Shop/Admin/Articles/list.blade.php | 4 -- .../partials/prices/block_price.blade.php | 2 +- .../views/Shop/Admin/TagGroups/list.blade.php | 4 -- 19 files changed, 116 insertions(+), 55 deletions(-) create mode 100644 resources/views/Shop/Admin/ArticleAttributeValues/index.blade.php diff --git a/app/DataTables/Shop/ArticlesDataTable.php b/app/DataTables/Shop/ArticlesDataTable.php index 0c900f1f..89409ba2 100644 --- a/app/DataTables/Shop/ArticlesDataTable.php +++ b/app/DataTables/Shop/ArticlesDataTable.php @@ -13,30 +13,15 @@ class ArticlesDataTable extends DataTable public function query(Article $model) { // $model = $model::with('Family')->select('shop_articles.*','family.name as family_name')->join('shop_article_families as family', 'family.id', '=', 'shop_articles.article_family_id')->groupBy('shop_articles.id'); - $model = $model::with('Family')->select('shop_articles.*'); + $model = $model::with('article_family')->select('shop_articles.*'); // $model = $model::joinRelations('Family')->select('shop_articles.*','shop_article_families.name as family_name'); return self::buildQuery($model); } protected function getColumns() { - /* - $columns = []; - $columns[] = [ - 'name' => 'family', - 'data' => 'family.name', - 'title' => 'Famille', - 'orderable' => false - ]; - $columns[] = [ - 'name' => 'name', - 'title' => 'Nom', - ]; - $columns[] = self::makeColumnButtons(); - return $columns; - */ return [ - Column::make('family.name')->title('Famille')->orderable(false), + Column::make('article_family.name')->title('Famille')->orderable(false), Column::make('name')->title('Nom'), self::makeColumnButtons(), ]; diff --git a/app/Http/Controllers/Shop/Admin/ArticleAttributeFamilyController.php b/app/Http/Controllers/Shop/Admin/ArticleAttributeFamilyController.php index 6958909b..98a0a7bb 100644 --- a/app/Http/Controllers/Shop/Admin/ArticleAttributeFamilyController.php +++ b/app/Http/Controllers/Shop/Admin/ArticleAttributeFamilyController.php @@ -12,7 +12,7 @@ class ArticleAttributeFamilyController extends Controller { public function index(ArticleAttributeFamiliesDataTable $dataTable) { - return $dataTable->render('Shop.Admin.ArticleAttributeFamilies.index'); + return $dataTable->render('Shop.Admin.ArticleAttributeFamilies.list'); } public function getDatatable(Request $request) diff --git a/app/Models/Shop/Article.php b/app/Models/Shop/Article.php index ec7b8971..1311d445 100644 --- a/app/Models/Shop/Article.php +++ b/app/Models/Shop/Article.php @@ -46,4 +46,9 @@ class Article extends Model implements HasMedia return $this->belongsTo($this->model, 'model_id'); } + public function scopeByArticle($query, $id) + { + return $query->where('shop_articles.id',$id); + } + } \ No newline at end of file diff --git a/app/Models/Shop/ArticlePrice.php b/app/Models/Shop/ArticlePrice.php index 863fa12b..c30f866b 100644 --- a/app/Models/Shop/ArticlePrice.php +++ b/app/Models/Shop/ArticlePrice.php @@ -3,9 +3,12 @@ namespace App\Models\Shop; use Illuminate\Database\Eloquent\Model; +use Znck\Eloquent\Traits\BelongsToThrough; class ArticlePrice extends Model { + use BelongsToThrough; + protected $guarded = ['id']; protected $table = 'shop_article_prices'; @@ -16,7 +19,21 @@ class ArticlePrice extends Model public function article() { - return $this->belongsTo('App\Models\Shop\Article'); + return $this->belongsToThrough( + 'App\Models\Shop\Article', + 'App\Models\Shop\ArticleAttribute', + null, + '', + ['App\Models\Shop\Article' => 'article_id', 'App\Models\Shop\ArticleAttribute' => 'article_attribute_id'] + ); + } + + public function scopeByArticle($query, $id) + { + return $query->whereHas('article', function ($query) use ($id) { + $query->byArticle($id); + }); + } } \ No newline at end of file diff --git a/app/Models/Shop/Category.php b/app/Models/Shop/Category.php index facf8140..054fb977 100644 --- a/app/Models/Shop/Category.php +++ b/app/Models/Shop/Category.php @@ -12,8 +12,7 @@ use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin; class Category extends Model { - use Taggable; - use HasMediaTrait; + use Taggable, HasMediaTrait; protected $guarded = ['id']; protected $table = 'shop_categories'; diff --git a/app/Repositories/Shop/ArticleAttributes.php b/app/Repositories/Shop/ArticleAttributes.php index 8af8bdb0..7045a191 100644 --- a/app/Repositories/Shop/ArticleAttributes.php +++ b/app/Repositories/Shop/ArticleAttributes.php @@ -25,17 +25,27 @@ class ArticleAttributes return ArticleAttribute::find($id); } - public static function storeAttributes($article_price_id, $attributes) + public static function getByArticle($id) + { + return ArticleAttribute::byArticle($id)->get(); + } + + public static function getByArticleWithPrices($id) + { + return ArticleAttribute::with('prices')->byArticle($id)->get(); + } + + public static function storeAttributes($article_id, $attributes) { foreach ($attributes as $key => $attribute) { - self::storeAttribute($article_price_id, $attributes[$key]); + self::storeAttribute($article_id, $attributes[$key]); } } - public static function storeAttribute($article_price_id, $attribute) + public static function storeAttribute($article_id, $attribute) { - $attribute['article_price_id'] = $article_price_id; + $attribute['article_id'] = $article_id; unset($attribute['attribute_family_id']); return self::store($attribute); } diff --git a/app/Repositories/Shop/ArticlePrices.php b/app/Repositories/Shop/ArticlePrices.php index 3ce00e22..42a6b0e9 100644 --- a/app/Repositories/Shop/ArticlePrices.php +++ b/app/Repositories/Shop/ArticlePrices.php @@ -13,9 +13,14 @@ use App\Models\Shop\ArticlePrice; class ArticlePrices { - public static function getPricesByArticle($id) + public static function getByArticle($id) { - return ArticlePrice::with('ArticleAttributes.Value')->byArticle($id)->get()->toArray(); + return ArticlePrice::byArticle($id)->get(); + } + + public static function getByArticleWithAttribute($id) + { + return ArticlePrice::with('article_attribute')->byArticle($id)->get(); } public static function getDatatable() @@ -34,11 +39,11 @@ class ArticlePrices return ArticlePrice::find($id); } - public static function storePrices($article_id, $prices) + public static function storePrices($article_attribute_id, $prices) { if ($prices) { foreach ($prices as $key => $price) { - $prices[$key]['article_id'] = $article_id; + $prices[$key]['article_attribute_id'] = $article_attribute_id; self::store($prices[$key]); } } else { diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 7c192c49..528d1abf 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -30,6 +30,7 @@ class Articles $data = $article->toArray(); $data['categories'] = self::getCategoriesByArticle($article); $data['tags'] = self::getTagsByArticle($article); + // $data['attributes'] = self::getAttributesByArticle($article); $data['prices'] = self::getPricesByArticle($article); self::getMeta($data); return $data; @@ -56,9 +57,14 @@ class Articles return $article->tags->pluck('id')->toArray(); } + public static function getAttributesByArticle($article) + { + return ArticleAttributes::getByArticleWithPrices($article->id)->toArray(); + } + public static function getPricesByArticle($article) { - return ArticlePrices::getPricesByArticle($article->id); + return ArticlePrices::getByArticleWithAttribute($article->id)->toArray(); } public static function get($id) diff --git a/composer.json b/composer.json index 78216a17..9fd56af1 100644 --- a/composer.json +++ b/composer.json @@ -76,6 +76,7 @@ "spatie/laravel-activitylog": "^3.6", "spatie/laravel-backup": "^6.2", "spatie/laravel-medialibrary": "^7.0", + "staudenmeir/belongs-to-through": "^2.5", "staudenmeir/eloquent-has-many-deep": "^1.8", "stillat/numeral.php": "^2.0", "te7a-houdini/laroute": "^1.0", diff --git a/resources/views/Botanic/Admin/Families/list.blade.php b/resources/views/Botanic/Admin/Families/list.blade.php index dfdeb3a5..2361800f 100644 --- a/resources/views/Botanic/Admin/Families/list.blade.php +++ b/resources/views/Botanic/Admin/Families/list.blade.php @@ -6,6 +6,6 @@ @section('content') @component('components.card') - @include('components.datatable', ['route' => route('Botanic.Admin.Families.index'), 'model' => 'families']) + @include('components.datatable', ['route' => route('Botanic.Admin.Families.index'), 'model' => 'Families']) @endcomponent @endsection \ No newline at end of file diff --git a/resources/views/Shop/Admin/ArticleAttributeFamilies/index.blade.php b/resources/views/Shop/Admin/ArticleAttributeFamilies/index.blade.php index 526bffab..526a0781 100644 --- a/resources/views/Shop/Admin/ArticleAttributeFamilies/index.blade.php +++ b/resources/views/Shop/Admin/ArticleAttributeFamilies/index.blade.php @@ -10,10 +10,10 @@