|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\Product; |
| 6 | +use Illuminate\Http\Request; |
| 7 | + |
| 8 | +class ProductController extends Controller |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Display a listing of the resource. |
| 12 | + * |
| 13 | + * @return \Illuminate\Http\Response |
| 14 | + */ |
| 15 | + public function index() |
| 16 | + { |
| 17 | + $products = Product::latest()->paginate(); |
| 18 | + |
| 19 | + return view('products.index', compact('products')); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Show the form for creating a new resource. |
| 24 | + * |
| 25 | + * @return \Illuminate\Http\Response |
| 26 | + */ |
| 27 | + public function create() |
| 28 | + { |
| 29 | + return view('products.create'); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Store a newly created resource in storage. |
| 34 | + * |
| 35 | + * @param \Illuminate\Http\Request $request |
| 36 | + * @return \Illuminate\Http\Response |
| 37 | + */ |
| 38 | + public function store(Request $request) |
| 39 | + { |
| 40 | + $request->validate([ |
| 41 | + 'title' => 'required|min:3', |
| 42 | + 'description' => 'required|min:3', |
| 43 | + 'stock' => 'required', |
| 44 | + ]); |
| 45 | + |
| 46 | + $product = Product::create($request->all()); |
| 47 | + |
| 48 | + return redirect()->route('products.show', $product->id); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Display the specified resource. |
| 53 | + * |
| 54 | + * @param \App\Models\Product $product |
| 55 | + * @return \Illuminate\Http\Response |
| 56 | + */ |
| 57 | + public function show(Product $product) |
| 58 | + { |
| 59 | + return view('products.show', compact('product')); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Show the form for editing the specified resource. |
| 64 | + * |
| 65 | + * @param \App\Models\Product $product |
| 66 | + * @return \Illuminate\Http\Response |
| 67 | + */ |
| 68 | + public function edit(Product $product) |
| 69 | + { |
| 70 | + return view('products.edit', compact('product')); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Update the specified resource in storage. |
| 75 | + * |
| 76 | + * @param \Illuminate\Http\Request $request |
| 77 | + * @param \App\Models\Product $product |
| 78 | + * @return \Illuminate\Http\Response |
| 79 | + */ |
| 80 | + public function update(Request $request, Product $product) |
| 81 | + { |
| 82 | + $request->validate([ |
| 83 | + 'title' => 'required|min:3', |
| 84 | + 'description' => 'required|min:3', |
| 85 | + 'stock' => 'required', |
| 86 | + ]); |
| 87 | + |
| 88 | + $product->update($request->all()); |
| 89 | + |
| 90 | + return redirect()->route('products.show', $product->id); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Remove the specified resource from storage. |
| 95 | + * |
| 96 | + * @param \App\Models\Product $product |
| 97 | + * @return \Illuminate\Http\Response |
| 98 | + */ |
| 99 | + public function destroy(Product $product) |
| 100 | + { |
| 101 | + $product->delete(); |
| 102 | + |
| 103 | + return redirect()->route('products.index'); |
| 104 | + } |
| 105 | +} |
0 commit comments