pai-sklep/app/Http/Controllers/ProductController.php
2021-12-08 18:25:48 +01:00

112 lines
3.1 KiB
PHP

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