pai-sklep/app/Http/Controllers/ProductController.php

128 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2021-09-05 02:22:17 +02:00
<?php
namespace App\Http\Controllers;
2021-12-09 12:13:11 +01:00
use App\Models\Category;
2021-12-08 18:25:48 +01:00
use App\Models\Image;
2021-09-05 02:22:17 +02:00
use App\Models\Product;
use Illuminate\Http\Request;
2021-10-28 11:19:42 +02:00
use Illuminate\Support\Facades\Log;
2021-09-05 02:22:17 +02:00
class ProductController extends Controller
{
public function index()
{
2021-10-28 11:19:42 +02:00
// TMP do something better at some point maybe idk
return redirect()->route("product.create");
2021-09-05 02:22:17 +02:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
2021-12-08 18:25:48 +01:00
$prod = new Product();
2021-12-09 12:13:11 +01:00
$categories = Category::all();
return view("product/edit", [ 'product' => $prod, 'categories' => $categories, 'new'=>true]);
2021-09-05 02:22:17 +02:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
2021-12-08 18:25:48 +01:00
$validated = $request->validate([
2021-10-28 11:19:42 +02:00
'name'=>'required',
'description'=>'required',
2021-12-08 18:25:48 +01:00
'price'=>'required|numeric',
'visualization' => 'image|mimes:jpg,png,jpeg,gif,svg,webp|max:5000',
2021-10-28 11:19:42 +02:00
]);
2021-12-08 18:25:48 +01:00
$product = new Product($validated);
2021-12-09 12:13:11 +01:00
if($validated['category']) {
$product->categories()->sync($validated['category']);
}
2021-10-28 11:19:42 +02:00
$product->save();
2021-12-08 18:25:48 +01:00
if (isset($validated["visualization"])) {
$visualization = new Image();
$visualization->path = $request->file('image')->store("uploads");
$product->images()->save($visualization);
}
2021-10-28 11:19:42 +02:00
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
2021-09-05 02:22:17 +02:00
}
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
2021-12-08 18:25:48 +01:00
return view("product.view", ["product" => $product]);
2021-09-05 02:22:17 +02:00
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
2021-12-09 12:13:11 +01:00
$categories = Category::all();
return view("product.edit", ["product" => $product, 'categories' => $categories, 'new'=>false]);
2021-09-05 02:22:17 +02:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
2021-12-09 12:13:11 +01:00
$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']);
}
2021-10-28 11:19:42 +02:00
$product->save();
2021-12-08 18:25:48 +01:00
if (isset($request["image"])) {
$visualization = new Image();
$visualization->path = $request->file('image')->store("uploads");
$product->images()->save($visualization);
}
2021-10-28 11:19:42 +02:00
return redirect()->route("product.show", [ 'product' => $product])->with("message", "Product created successfully");
2021-09-05 02:22:17 +02:00
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
2021-10-28 11:19:42 +02:00
$product->delete();
return redirect()->route("product.index");
2021-09-05 02:22:17 +02:00
}
}