# 4.5 Delete Product (ok)

<figure><img src="https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCBeDUD-PK_RF-YgJRe%2Fuploads%2F7PIDwS2Imh4V0rzOnykr%2Fimage.png?alt=media&#x26;token=74f2c66c-8e77-45c8-addb-f7bf096625b3" alt=""><figcaption></figcaption></figure>

C:\xampp82\htdocs\phongkhamnet\app\Http\Controllers\ProductController.php

```php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
  /**
   * Display a listing of the resource.
   */
  public function index()
  {
    $products = Product::latest()->paginate(5);
    return view('products.index', compact('products'))->with('i', (request()->input('page', 1) - 1) * 5);
  }
  /**
   * Show the form for creating a new resource.
   */
  public function create()
  {
    return view('products.create');
  }
  /**
   * Store a newly created resource in storage.
   */
  public function store(Request $request)
  {
    $input = $request->all();
    if ($image = $request->file('image')) {
      $destinationPath = 'image/';
      $productImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
      $image->move($destinationPath, $productImage);
      $input['image'] = "$productImage";
    }
    Product::create($input);
    return redirect()->route('products.index');
  }
  /**
   * Display the specified resource.
   */
  public function show(Product $product)
  {
    return view('products.show', compact('product'));
  }
  /**
   * Show the form for editing the specified resource.
   */
  public function edit(Product $product)
  {
    return view('products.edit', compact('product'));
  }
  /**
   * Update the specified resource in storage.
   */
  public function update(Request $request, Product $product)
  {
    $input = $request->all();
    if ($image = $request->file('image')) {
      $destinationPath = 'image/';
      $productImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
      $image->move($destinationPath, $productImage);
      $input['image'] = "$productImage";
    } else {
      unset($input['image']);
    }
    $product->update($input);
    return redirect()->route('products.index')->with('success', 'Product updated successfully');
  }
  /**
   * Remove the specified resource from storage.
   */
  public function destroy(Product $product)
  {
    $ImagePath = 'image/' . $product->image;
    unlink($ImagePath);
    $product->delete();
    return redirect()->route('products.index');
  }
}

```
