<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Product; use Illuminate\Http\Request; use Illuminate\Support\Str; class ProductController extends Controller { public function index() { $products = Product::latest()->paginate(10); return view('admin.products.index', compact('products')); } public function create() { $product = new Product(); return view('admin.products.create', compact('product')); } public function store(Request $request) { $data = $request->validate([ 'name' => ['required', 'string', 'max:200'], 'slug' => ['nullable', 'string', 'max:200', 'unique:products,slug'], 'short_description' => ['nullable', 'string'], 'description' => ['nullable', 'string'], 'video_url' => ['nullable', 'string', 'max:500'], 'price_cents' => ['required', 'integer', 'min:0'], 'is_active' => ['required'], ]); $data['is_active'] = filter_var($data['is_active'], FILTER_VALIDATE_BOOLEAN); $data['slug'] = $data['slug'] ? Str::slug($data['slug']) : Str::slug($data['name']); Product::create($data); return redirect()->route('admin.products.index')->with('success', 'Product created.'); } public function edit(Product $product) { return view('admin.products.edit', compact('product')); } public function update(Request $request, Product $product) { $data = $request->validate([ 'name' => ['required', 'string', 'max:200'], 'slug' => ['nullable', 'string', 'max:200', 'unique:products,slug,' . $product->id], 'short_description' => ['nullable', 'string'], 'description' => ['nullable', 'string'], 'video_url' => ['nullable', 'string', 'max:500'], 'price_cents' => ['required', 'integer', 'min:0'], 'is_active' => ['required'], ]); $data['is_active'] = filter_var($data['is_active'], FILTER_VALIDATE_BOOLEAN); $data['slug'] = $data['slug'] ? Str::slug($data['slug']) : Str::slug($data['name']); $product->update($data); return redirect()->route('admin.products.index')->with('success', 'Product updated.'); } public function destroy(Product $product) { $product->delete(); return redirect()->route('admin.products.index')->with('success', 'Product deleted.'); } }