From e5c047cf6e9474b5d0c88979b78e1fe0bdf27823 Mon Sep 17 00:00:00 2001 From: bad Date: Thu, 9 Dec 2021 12:13:11 +0100 Subject: [PATCH] Add category selection to product edit --- app/Http/Controllers/ProductController.php | 23 ++++++++++++++++++---- app/Models/Category.php | 2 +- app/Models/Product.php | 3 ++- resources/views/product/edit.blade.php | 6 ++++++ resources/views/product/view.blade.php | 1 - 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 98d9fab..5e00c93 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Category; use App\Models\Image; use App\Models\Product; use Illuminate\Http\Request; @@ -23,7 +24,8 @@ class ProductController extends Controller public function create() { $prod = new Product(); - return view("product/edit", [ 'product' => $prod,'new'=>true]); + $categories = Category::all(); + return view("product/edit", [ 'product' => $prod, 'categories' => $categories, 'new'=>true]); } /** @@ -42,6 +44,9 @@ class ProductController extends Controller ]); $product = new Product($validated); + if($validated['category']) { + $product->categories()->sync($validated['category']); + } $product->save(); if (isset($validated["visualization"])) { @@ -73,7 +78,8 @@ class ProductController extends Controller */ public function edit(Product $product) { - return view("product.edit", ["product" => $product, 'new'=>false]); + $categories = Category::all(); + return view("product.edit", ["product" => $product, 'categories' => $categories, 'new'=>false]); } /** @@ -85,7 +91,17 @@ class ProductController extends Controller */ public function update(Request $request, Product $product) { - $product->fill($request->all()); + $validated = $request->validate([ + 'name'=>'string', + 'description'=>'string', + 'price'=>'numeric', + 'category' => 'array|exists:App\Models\Category,uuid', + 'visualization' => 'image|mimes:jpg,png,jpeg,gif,svg,webp|max:5000', + ]); + $product->fill($validated); + if($validated['category']) { + $product->categories()->sync($validated['category']); + } $product->save(); if (isset($request["image"])) { $visualization = new Image(); @@ -94,7 +110,6 @@ class ProductController extends Controller $product->images()->save($visualization); } - //dd($product, $request, $visualization); return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully"); } diff --git a/app/Models/Category.php b/app/Models/Category.php index ccb2ed6..31da978 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -13,7 +13,7 @@ class Category extends Model protected $fillable = ['name']; public function products() { - return $this->belongsToMany(Category::class,'categories_products','category_uuid'); + return $this->belongsToMany(Product::class,'categories_products','category_uuid'); } public function image() { diff --git a/app/Models/Product.php b/app/Models/Product.php index 13a7b41..bcc6103 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -23,6 +23,7 @@ class Product extends Model 'name', 'description', 'images', - 'price' + 'price', + 'categories' ]; } diff --git a/resources/views/product/edit.blade.php b/resources/views/product/edit.blade.php index 35bf8d2..7543773 100644 --- a/resources/views/product/edit.blade.php +++ b/resources/views/product/edit.blade.php @@ -12,6 +12,12 @@ + + @foreach($categories as $category) + products()->find($product)) checked @endif> + + @endforeach + @if(!$new) diff --git a/resources/views/product/view.blade.php b/resources/views/product/view.blade.php index 42f6dea..6647e35 100644 --- a/resources/views/product/view.blade.php +++ b/resources/views/product/view.blade.php @@ -14,7 +14,6 @@
- {{ dd($product->categories[0]->name) }}

{{ $product->name }}

{{ ($product->price) }}