2026-09-16 11:43:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
use App\Http\Requests\StoreProductRequest;
|
|
|
|
|
use App\Http\Requests\UpdateProductRequest;
|
2026-10-21 00:48:38 +02:00
|
|
|
use App\Models\Product;
|
2026-09-16 11:43:27 +02:00
|
|
|
use Illuminate\Http\Request;
|
2026-10-21 00:48:38 +02:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-09-16 11:43:27 +02:00
|
|
|
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
|
{
|
2026-10-07 00:48:56 +02:00
|
|
|
// TP6 : recherche par nom ou prix (bonus), avec pagination de dix produits.
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$searchInput = $request->query('search', '');
|
|
|
|
|
$search = is_string($searchInput) ? trim($searchInput) : '';
|
|
|
|
|
$query = Product::query();
|
2026-09-16 11:43:27 +02:00
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
if ($search !== '') {
|
|
|
|
|
$query->where(function ($query) use ($search) {
|
|
|
|
|
$query->where('name', 'like', '%'.$search.'%')
|
|
|
|
|
->orWhere('price', 'like', '%'.$search.'%');
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-09-16 12:08:49 +02:00
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
$products = $query->orderBy('id')->paginate(10)->withQueryString();
|
2026-09-16 12:08:49 +02:00
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
return view('products.index', compact('products', 'search'));
|
2026-09-16 11:43:27 +02:00
|
|
|
}
|
2026-10-07 00:48:56 +02:00
|
|
|
|
|
|
|
|
// TP4 : les six autres actions complètent le CRUD des produits.
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2026-10-21 00:48:38 +02:00
|
|
|
Gate::authorize('create', Product::class);
|
|
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
return view('products.create');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TP6 : seules les données validées par le Form Request sont enregistrées.
|
|
|
|
|
public function store(StoreProductRequest $request)
|
|
|
|
|
{
|
2026-10-21 00:48:38 +02:00
|
|
|
Gate::authorize('create', Product::class);
|
2026-10-07 00:48:56 +02:00
|
|
|
Product::create($request->validated());
|
|
|
|
|
|
|
|
|
|
return redirect()->route('products.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show(Product $product)
|
|
|
|
|
{
|
2026-10-21 00:48:38 +02:00
|
|
|
Gate::authorize('view', $product);
|
|
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
return view('products.show', compact('product'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(Product $product)
|
|
|
|
|
{
|
2026-10-21 00:48:38 +02:00
|
|
|
Gate::authorize('update', $product);
|
|
|
|
|
|
2026-10-07 00:48:56 +02:00
|
|
|
return view('products.edit', compact('product'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(UpdateProductRequest $request, Product $product)
|
|
|
|
|
{
|
2026-10-21 00:48:38 +02:00
|
|
|
Gate::authorize('update', $product);
|
2026-10-07 00:48:56 +02:00
|
|
|
$product->update($request->validated());
|
|
|
|
|
|
|
|
|
|
return redirect()->route('products.index');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function destroy(Product $product)
|
|
|
|
|
{
|
2026-10-21 00:48:38 +02:00
|
|
|
// TP8 : la Policy réserve la suppression aux administrateurs.
|
|
|
|
|
Gate::authorize('delete', $product);
|
2026-10-07 00:48:56 +02:00
|
|
|
$product->delete();
|
|
|
|
|
|
|
|
|
|
return redirect()->route('products.index');
|
|
|
|
|
}
|
|
|
|
|
}
|