Add category selection to product edit
This commit is contained in:
parent
f46280eb2b
commit
e5c047cf6e
5 changed files with 28 additions and 7 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Category;
|
||||||
use App\Models\Image;
|
use App\Models\Image;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
@ -23,7 +24,8 @@ class ProductController extends Controller
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
$prod = new Product();
|
$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);
|
$product = new Product($validated);
|
||||||
|
if($validated['category']) {
|
||||||
|
$product->categories()->sync($validated['category']);
|
||||||
|
}
|
||||||
$product->save();
|
$product->save();
|
||||||
|
|
||||||
if (isset($validated["visualization"])) {
|
if (isset($validated["visualization"])) {
|
||||||
|
@ -73,7 +78,8 @@ class ProductController extends Controller
|
||||||
*/
|
*/
|
||||||
public function edit(Product $product)
|
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)
|
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();
|
$product->save();
|
||||||
if (isset($request["image"])) {
|
if (isset($request["image"])) {
|
||||||
$visualization = new Image();
|
$visualization = new Image();
|
||||||
|
@ -94,7 +110,6 @@ class ProductController extends Controller
|
||||||
|
|
||||||
$product->images()->save($visualization);
|
$product->images()->save($visualization);
|
||||||
}
|
}
|
||||||
//dd($product, $request, $visualization);
|
|
||||||
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
|
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ class Category extends Model
|
||||||
protected $fillable = ['name'];
|
protected $fillable = ['name'];
|
||||||
|
|
||||||
public function products() {
|
public function products() {
|
||||||
return $this->belongsToMany(Category::class,'categories_products','category_uuid');
|
return $this->belongsToMany(Product::class,'categories_products','category_uuid');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function image() {
|
public function image() {
|
||||||
|
|
|
@ -23,6 +23,7 @@ class Product extends Model
|
||||||
'name',
|
'name',
|
||||||
'description',
|
'description',
|
||||||
'images',
|
'images',
|
||||||
'price'
|
'price',
|
||||||
|
'categories'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,12 @@
|
||||||
<textarea name="description">{{ $product->description }}</textarea>
|
<textarea name="description">{{ $product->description }}</textarea>
|
||||||
<input type="number" name="price" value="{{ $product->price }}">
|
<input type="number" name="price" value="{{ $product->price }}">
|
||||||
<input type="file" name="image">
|
<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>
|
<button type="submit">Zatwierdź</button>
|
||||||
|
|
||||||
@if(!$new)
|
@if(!$new)
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
<div class="product-container">
|
<div class="product-container">
|
||||||
<div class="product-container-left">
|
<div class="product-container-left">
|
||||||
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ asset("test.txt") }} @endif">
|
<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">
|
<div class="name-price-container">
|
||||||
<h2 class="product-name"> {{ $product->name }} </h2>
|
<h2 class="product-name"> {{ $product->name }} </h2>
|
||||||
<h3>{{ ($product->price) }}<span class="price-currency">zł</span></h3>
|
<h3>{{ ($product->price) }}<span class="price-currency">zł</span></h3>
|
||||||
|
|
Loading…
Reference in a new issue