Add category selection to product edit

This commit is contained in:
bad 2021-12-09 12:13:11 +01:00
parent f46280eb2b
commit e5c047cf6e
5 changed files with 28 additions and 7 deletions

View file

@ -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");
}

View file

@ -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() {

View file

@ -23,6 +23,7 @@ class Product extends Model
'name',
'description',
'images',
'price'
'price',
'categories'
];
}

View file

@ -12,6 +12,12 @@
<textarea name="description">{{ $product->description }}</textarea>
<input type="number" name="price" value="{{ $product->price }}">
<input type="file" name="image">
@foreach($categories as $category)
<input type="checkbox" id="category_{{$loop->index}}" name="category[{{$loop->index}}]" value="{{$category->uuid}}" @if($category->products()->find($product)) checked @endif>
<label for="category_{{$loop->index}}">{{$category->name}}</label>
@endforeach
<button type="submit">Zatwierdź</button>
@if(!$new)

View file

@ -14,7 +14,6 @@
<div class="product-container">
<div class="product-container-left">
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ asset("test.txt") }} @endif">
{{ dd($product->categories[0]->name) }}
<div class="name-price-container">
<h2 class="product-name"> {{ $product->name }} </h2>
<h3>{{ ($product->price) }}<span class="price-currency"></span></h3>