From 5bd09966b7044511224198d1520c0d6581d8727e Mon Sep 17 00:00:00 2001 From: Ludovic CANDELLIER Date: Mon, 15 Jun 2020 00:17:15 +0200 Subject: [PATCH] [WIP] Fix on prices & attributes --- .../Shop/Admin/ArticleController.php | 14 +------ app/Models/Shop/ArticleAttribute.php | 3 ++ app/Models/Shop/ArticlePrice.php | 1 + app/Repositories/Shop/ArticleAttributes.php | 11 ++++-- app/Repositories/Shop/ArticlePrices.php | 13 ++++--- app/Repositories/Shop/Articles.php | 37 +++++++++++++++---- 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/Shop/Admin/ArticleController.php b/app/Http/Controllers/Shop/Admin/ArticleController.php index 59622d9c..168fef21 100644 --- a/app/Http/Controllers/Shop/Admin/ArticleController.php +++ b/app/Http/Controllers/Shop/Admin/ArticleController.php @@ -29,19 +29,7 @@ class ArticleController extends Controller public function store(Request $request) { $data = $request->all(); - $images = isset($data['images']) ? $data['images'] : false; - $categories = isset($data['categories']) ? $data['categories'] : false; - $tags = isset($data['tags']) ? $data['tags'] : false; - $prices = isset($data['prices']) ? $data['prices'] : false; - unset($data['images']); - unset($data['categories']); - unset($data['tags']); - unset($data['prices']); - $article = Articles::store($data); - Articles::storeImages($article, $images); - Articles::storeCategories($article, $categories); - Articles::storeTags($article, $categories); - Articles::storePrices($article, $prices); + Articles::storeFull($data); return redirect()->route('Shop.Admin.Articles.index'); } diff --git a/app/Models/Shop/ArticleAttribute.php b/app/Models/Shop/ArticleAttribute.php index 5387a0c3..a4c3938e 100644 --- a/app/Models/Shop/ArticleAttribute.php +++ b/app/Models/Shop/ArticleAttribute.php @@ -6,6 +6,9 @@ use Illuminate\Database\Eloquent\Model; class ArticleAttribute extends Model { + protected $guarded = ['id']; + protected $table = 'shop_article_attribute_attributes'; + public function Price() { return $this->belongsTo('App\Models\Shop\ArticlePrice'); diff --git a/app/Models/Shop/ArticlePrice.php b/app/Models/Shop/ArticlePrice.php index f19ba53e..fa98ab55 100644 --- a/app/Models/Shop/ArticlePrice.php +++ b/app/Models/Shop/ArticlePrice.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; class ArticlePrice extends Model { protected $guarded = ['id']; + protected $table = 'shop_article_prices'; public function Article() { diff --git a/app/Repositories/Shop/ArticleAttributes.php b/app/Repositories/Shop/ArticleAttributes.php index f0983863..73338cbb 100644 --- a/app/Repositories/Shop/ArticleAttributes.php +++ b/app/Repositories/Shop/ArticleAttributes.php @@ -29,12 +29,17 @@ class ArticleAttributes { foreach ($attributes as $key => $attribute) { - $attributes[$key]['article_price_id'] = $article_price_id; - unset($attributes[$key]['attribute_family_id']); - self::store($attributes[$key]); + self::storeAttribute($article_price_id, $attributes[$key]); } } + public static function storeAttribute($article_price_id, $attribute) + { + $attribute['article_price_id'] = $article_price_id; + unset($attribute['attribute_family_id']); + return self::store($attribute); + } + public static function store($data) { $id = isset($data['id']) ? $data['id'] : false; diff --git a/app/Repositories/Shop/ArticlePrices.php b/app/Repositories/Shop/ArticlePrices.php index 860f3faf..99091644 100644 --- a/app/Repositories/Shop/ArticlePrices.php +++ b/app/Repositories/Shop/ArticlePrices.php @@ -32,7 +32,7 @@ class ArticlePrices public static function storePrices($article_id, $prices) { if ($prices) { - foreach ($prices as $$key => $price) { + foreach ($prices as $key => $price) { $prices[$key]['article_id'] = $article_id; self::store($prices[$key]); } @@ -41,11 +41,6 @@ class ArticlePrices } } - public static function storeAttributes($article_price_id,$attributes) - { - return ArticleAttributes::storeAttributes($article_price_id, $attributes); - } - public static function store($data) { $attributes = isset($data['attributes']) ? $data['attributes'] : false; @@ -59,6 +54,12 @@ class ArticlePrices return $price->id; } + public static function storeAttributes($article_price_id,$attributes) + { + return ArticleAttributes::storeAttribute($article_price_id, $attributes); + } + + public static function create($data) { return ArticlePrice::create($data); diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 5ec069b7..1ef05500 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -60,6 +60,24 @@ class Articles return Article::find($id); } + public static function storeFull($data) + { + $images = isset($data['images']) ? $data['images'] : false; + $categories = isset($data['categories']) ? $data['categories'] : false; + $tags = isset($data['tags']) ? $data['tags'] : false; + $prices = isset($data['prices']) ? $data['prices'] : false; + unset($data['images']); + unset($data['categories']); + unset($data['tags']); + unset($data['prices']); + $article = self::store($data); + self::storeImages($article, $images); + self::storeCategories($article, $categories); + self::storeTags($article, $tags); + self::storePrices($article, $prices); + return $article; + } + public static function store($data) { $id = isset($data['id']) ? $data['id'] : false; @@ -86,21 +104,24 @@ class Articles public static function storeCategories($article, $categories) { - if ($categories) - { + if ($categories) { $categories = collect($categories)->transform(function ($item, $key) { return (int) $item; })->toArray(); - return $article->attachCategories($categories); - } else return false; + return $article->syncCategories($categories, true); + } else { + return false; + } } public static function storeTags($article, $tags) { - if ($tags) - { - return $article->attachTags($tags); - } + if ($tags) { + $tags = collect($tags)->transform(function ($item, $key) { + return (int) $item; + })->toArray(); + return $article->syncTags($tags, true); + } else return false; } public static function storePrices($article, $prices)