Advertisement
msyazwans

ProductController.php

Jun 7th, 2024
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | Source Code | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Models\Product;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Response;
  9. use Illuminate\View\View;
  10. use App\Http\Requests\ProductStoreRequest;
  11. use App\Http\Requests\ProductUpdateRequest;
  12.  
  13.  
  14. class ProductController extends Controller
  15. {
  16.     /**
  17.      * Display a listing of the resource.
  18.      */
  19.     public function index()
  20.     {
  21.         //
  22.         //$products = Product::all();
  23.         //return view('products.index', compact('products'));
  24.  
  25.         $products = Product::latest()->paginate(5);
  26.  
  27.         return view('products.index', compact('products'))
  28.             ->with('i', (request()->input('page', 1) - 1) * 5);
  29.  
  30.     }
  31.  
  32.     /**
  33.      * Show the form for creating a new resource.
  34.      */
  35.     public function create()
  36.     {
  37.         //
  38.         return view('products.create');
  39.     }
  40.  
  41.     /**
  42.      * Store a newly created resource in storage.
  43.      */
  44.     public function store(ProductStoreRequest $request)
  45.     {
  46.         //
  47.         Product::create($request->validated());
  48.  
  49.         return redirect()->route('products.index')
  50.                          ->with('success', 'Product created successfully.');
  51.     }
  52.  
  53.     /**
  54.      * Display the specified resource.
  55.      */
  56.     public function show(Product $product)
  57.     {
  58.         //
  59.         return view('products.show',compact('product'));
  60.     }
  61.  
  62.     /**
  63.      * Show the form for editing the specified resource.
  64.      */
  65.     public function edit(Product $product)
  66.     {
  67.         //
  68.         return view('products.edit',compact('product'));
  69.     }
  70.  
  71.     /**
  72.      * Update the specified resource in storage.
  73.      */
  74.     public function update(ProductUpdateRequest $request, Product $product)
  75.     {
  76.         //
  77.         $product->update($request->validated());
  78.  
  79.         return redirect()->route('products.index')
  80.                         ->with('success','Product updated successfully');
  81.     }
  82.  
  83.     /**
  84.      * Remove the specified resource from storage.
  85.      */
  86.     public function destroy(Product $product)
  87.     {
  88.         //
  89.         $product->delete();
  90.  
  91.         return redirect()->route('products.index')
  92.                         ->with('success','Product deleted successfully');
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement