Step 01: In ProductController, adding edit() method
public function edit( Product $product ) { $meta_title = 'Edit Product ' . $product->name; return view( 'products.edit', compact( 'meta_title', 'product' ) ); }
Step 02: Adding edit.blade.php under resources/view/products folder inside section blade directive extending app layout
<div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <h2 class="float-start">Edit Product</h2> <div class="float-end"> <a class="btn btn-sm btn-primary" href="{{ route('products.index') }}"> Back to products</a> </div> <div class="clearfix"></div> <div class="card "> <div class="card-body"> <form method="post" action="{{ route('products.update', $product->id) }}" id="updatefrm"> @csrf @method('PUT') <div class="form-group mb-2"> <label for="title">Name</label> <br> <input name="name" class="form-control" placeholder="product name" title="Product Name" type="text" id="ProductName" value="{{ $product->name }}" /> @if ($errors->has('name')) <span class="text-danger">{{ $errors->first('name') }}</span> @endif </div> <div class="form-group mb-2"> <label for="title">Description</label> <br> <textarea name="description" class="form-control" placeholder="product desciption" id="product_desciption">{{ $product->description }}</textarea> </div> <div class="form-group mb-2"> <label for="title">Cost ($)</label> <br> <input name="cost" class="form-control" placeholder="Cost" title="Cost" type="text" id="cost" value="{{ $product->cost }}" /> @if ($errors->has('cost')) <span class="text-danger">{{ $errors->first('cost') }}</span> @endif </div> <div class="form-group mb-2"> <label for="title">Status?</label> <br> <div class="ml-4"> <input type="checkbox" id="status" name="status" {{ ($product->status == 1) ? 'checked' : '' }} class="form-check-input mb-2"> </div> @if ($errors->has('status')) <span class="text-danger">{{ $errors->first('status') }}</span> @endif </div> <input class="btn btn-sm btn-primary" class="form-control" type="submit" name="action" value="Edit"> </form> </div> </div> </div> </div> </div> </div>
Step 03: Back in ProductController adding update() method like below and keeping product to edit page after editing with flash message at the top
public function update( Product $product, ProductRequest $request ) { $data = [ 'name' => $request->name, 'user_id' => auth()->user()->id, 'description' => $request->description ?? '', 'cost' => $request->cost, 'status' => !empty($request->status) ? 1 : 0, ]; $product->update($data); return redirect()->route('products.edit',$product->id)->with('success', 'Product edited successfully'); }
Please note we have kept ProductRequest as paramater for update() method like it was in store() method so that same validation rule
applies on both of the methods.