2021-09-05 02:22:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
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();
|
|
|
|
return view("product/edit", [ 'product' => $prod,'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-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-08 18:25:48 +01:00
|
|
|
return view("product.edit", ["product" => $product, '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-10-28 11:19:42 +02:00
|
|
|
$product->fill($request->all());
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
//dd($product, $request, $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
|
|
|
}
|
|
|
|
}
|