Untitled
unknown
plain_text
a month ago
4.1 kB
4
Indexable
<?php namespace App\Http\Controllers; use App\Models\Product; use App\Models\Category; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class ProductController extends Controller { public function index() { $products = Product::with('category')->get(); return view('products.index', compact('products')); } public function create() { $categories = Category::all(); return view('products.create', compact('categories')); } public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'price' => 'required|numeric|min:0', 'quantity' => 'required|integer|min:1', 'category_id' => 'required|exists:categories,id', 'description' => 'nullable|string', 'discount' => 'nullable|numeric|min:0|max:100', 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048' ]); $data = $request->all(); // Lưu hình ảnh nếu có upload if ($request->hasFile('image')) { $imagePath = $request->file('image')->store('products', 'public'); $data['image'] = $imagePath; } Product::create($data); return redirect()->route('products.index')->with('success', 'Sản phẩm đã được thêm!'); } public function edit(Product $product) { $categories = Category::all(); return view('products.edit', compact('product', 'categories')); } public function update(Request $request, Product $product) { $request->validate([ 'name' => 'required|string|max:255', 'price' => 'required|numeric|min:0', 'quantity' => 'required|integer|min:1', 'category_id' => 'required|exists:categories,id', 'description' => 'nullable|string', 'discount' => 'nullable|numeric|min:0|max:100', 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048' ]); $data = $request->except('image'); if ($request->hasFile('image')) { if ($product->image) { Storage::disk('public')->delete($product->image); } $imagePath = $request->file('image')->store('products', 'public'); $data['image'] = $imagePath; } $product->update($data); return redirect()->route('products.index')->with('success', 'Cập nhật sản phẩm thành công!'); } public function destroy(Product $product) { if ($product->image) { Storage::disk('public')->delete($product->image); } $product->delete(); return redirect()->route('products.index')->with('success', 'Xóa sản phẩm thành công!'); } public function userProducts(Request $request) { $query = Product::query()->with('category'); // Tìm kiếm theo tên sản phẩm if ($request->has('search') && !empty($request->search)) { $query->where('name', 'like', '%' . $request->search . '%'); } // Lọc theo danh mục if ($request->has('category') && !empty($request->category)) { $query->where('category_id', $request->category); } // Lọc theo giá if ($request->has('min_price') && !empty($request->min_price)) { $query->where('price', '>=', $request->min_price); } if ($request->has('max_price') && !empty($request->max_price)) { $query->where('price', '<=', $request->max_price); } // Phân trang (10 sản phẩm mỗi trang) $products = $query->paginate(4); $categories = Category::all(); return view('products.products', compact('products', 'categories')); } }
Editor is loading...
Leave a Comment