testlara/app/Http/Controllers/ProductController.php
2023-10-27 19:59:05 +03:00

63 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ProductController extends Controller
{
public function index(): View
{
$products = Product::orderBy('id','desc')->paginate(5);
return view('product.index', compact('products'));
}
public function create(): View
{
return view('product.create');
}
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string',
'price' => 'required|numeric',
'article' => 'required|string',
'quantity' => 'required|integer',
]);
Product::create($request->post());
return redirect()->route('products.index')->with('success','Product has been created successfully.');
}
public function edit(Product $product): View
{
return view('product.edit',compact('product'));
}
public function update(Request $request, Product $product): RedirectResponse
{
$request->validate([
'name' => 'required|string',
'price' => 'required|numeric',
'quantity' => 'required|integer',
]);
$product->fill($request->post())->save();
return redirect()->route('products.index')->with('success','Product Has Been updated successfully');
}
public function destroy(Product $product): RedirectResponse
{
$product->delete();
return redirect()->route('products.index')->with('success','Company has been deleted successfully');
}
}