render('products/index', [ 'title' => 'Product Inventory', 'products' => $products, ]); } public function store(Request $request, Response $response): Response { $sku = trim((string)$request->post('sku')); $name = trim((string)$request->post('name')); $price = (float)$request->post('price', 0); $stock = (int)$request->post('stock', 0); $category = trim((string)$request->post('category', 'General')); $description = trim((string)$request->post('description', '')); if (empty($sku) || empty($name) || $price <= 0) { return $this->redirect('/products', null, 'Please provide valid SKU, name, and positive price.'); } try { Product::create([ 'sku' => $sku, 'name' => $name, 'price' => $price, 'stock' => $stock, 'category' => $category, 'description' => $description, ]); return $this->redirect('/products', 'Product added successfully!'); } catch (\Throwable $e) { return $this->redirect('/products', null, 'Error adding product: ' . $e->getMessage()); } } }