4.4 Edit Product (ok)

C:\xampp82\htdocs\phongkhamnet\app\Http\Controllers\ProductController.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)
  {
    //
  }
}

C:\xampp82\htdocs\phongkhamnet\resources\views\products\edit.blade.php

@extends('layouts.app')
@section('content')
<div class="row">
  <div class="col-lg-12 margin-tb">
    <div class="pull-left">
      <h2>Edit Product</h2>
    </div>
    <div class="pull-right">
      <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
    </div>
  </div>
</div>
@if ($errors->any())
<div class="alert alert-danger">
  <strong>Whoops!</strong> There were some problems with your input.<br><br>
  <ul>
    @foreach ($errors->all() as $error)
    <li>{{ $error }}</li>
    @endforeach
  </ul>
</div>
@endif
<form action="{{ route('products.update',$product->id) }}" method="POST" enctype="multipart/form-data">
  @csrf
  @method('PUT')
  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
      <div class="form-group">
        <strong>Name:</strong>
        <input type="text" name="name" value="{{ $product->name }}" class="form-control" placeholder="Name">
      </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12">
      <div class="form-group">
        <strong>Detail:</strong>
        <textarea class="form-control" style="height:150px" name="detail"
          placeholder="Detail">{{ $product->detail }}</textarea>
      </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12">
      <div class="form-group">
        <strong>Price:</strong>
        <input type="number" name="price" value="{{ $product->price }}" class="form-control" placeholder="Name">
      </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12">
      <div class="form-group">
        <strong>Image:</strong>
        <input type="file" name="image" class="form-control" placeholder="image">
        <img src="/image/{{ $product->image }}" width="300px">
      </div>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12 text-center">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>
<p class="text-center text-primary"><small>by Noor E Alam</small></p>
@endsection

Last updated