Laravel 12 E-Commerce: Implementing Admin Edit Brand
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. This video focuses on implementing the functionality to edit existing brands in the admin panel.
1. Controller Function and View Setup
Create Edit Function (`AdminController.php`)
First, create a function in the `AdminController` that accepts the brand'sID, finds the brand record, and passes it to the edit view:
public function edit_brand($id)
{
$brand = Brand::find($id);
return view("admin.brand-edit",compact("brand"));
}
Create Edit View (`brand-edit.blade.php`)
Create a new view file: `resources/views/admin/brand-edit.blade.php`. For efficiency, copy all content from `brand-add.blade.php` and paste it here.
2. Populate Form with Existing Brand Data
Modify the `brand-edit.blade.php` file to display the current brand data in the form fields and image preview.
Set Input Values
Set the `value` property for the Name and Slug input fields:
value="{{$brand->name}}"
value="{{$brand->slug}}"
Display Existing Image
Use conditional logic (`@if`) to display the existing brand image if it exists. If no image is present, hide the image preview:
<div class="mb-3">
@if($brand->image)
<img id="image-preview" src="{{asset("uploads/brands")}}/{{$brand->image}}" alt="Image Preview" style="max-width: 120px; height: auto;">
@else
<img id="image-preview" src="" alt="Image Preview" style="display: none; max-width: 120px; height: auto;">
@endif
</div>
The Remove Image button'sstyle should be set to `display: block;` when an image is present.
3. Update Brand Logic and Routing
Create Update Function (`AdminController.php`)
Create the `update_brand` function to handle the form submission. This function validates the incoming data, handles the image update (deleting the old image if a new one is uploaded), and saves the changes.
use IlluminateSupportFacadesFile;
public function update_brand(Request $request)
{
$request->validate([
"id" => "required",
"name" => "required",
"slug" => "required|unique:brands,slug," . $request->id,
"image" => "nullable|mimes:png,jpg,jpeg|max:2048"
]);
$brand = Brand::find($request->id);
$brand->name = $request->name;
$brand->slug = Str::slug($request->name);
if($request->hasFile("image")) {
// Delete old image if it exists
if(File::exists(public_path("uploads/brands") . "/" . $brand->image)) {
File::delete(public_path("uploads/brands") . "/" . $brand->image);
}
// Upload new image
$image = $request->file("image");
$file_extention = $request->file("image")->extension();
$file_name = Carbon::now()->timestamp . "." . $file_extention;
$this->GenerateBrandThumbailsImage($image, $file_name);
$brand->image = $file_name;
}
$brand->save();
return redirect()->route("admin.brands")->with("status", "Brand has been updated successfully!");
}
Define Route for Update (`web.php`)
Create the `GET` route for the edit page and the `PUT` route for the update logic:
Route::get("/admin/brand/edit/{id}", [AdminController::class, "edit_brand"])->name("admin.brand.edit");
Route::put("/admin/brand/update", [AdminController::class, "update_brand"])->name("admin.brand.update");
4. Final Form Updates in View
In `brand-edit.blade.php`, update the form'saction and method directives to point to the new update route:
<form action="{{route("admin.brand.update")}}" method="POST" enctype="multipart/form-data" data-parsley-validate>
@csrf
@method("PUT")
<!-- Include hidden input for ID -->
<input type="hidden" name="id" value="{{$brand->id}}">
<!-- ... other form fields ... -->
</form>
Also, update the Edit link in `brands.blade.php` to use the correct route and pass the brand ID:
<a href="{{route("admin.brand.edit",["id"=>$brand->id])}}">
<div class="item edit">
<i class="fa fa-edit"></i>
</div>
</a>
5. Testing
Refresh the Brands page, click on the Edit icon for a brand. You should see the edit page populated with the brand'scurrent data. Make changes and click Submit to test the update functionality.
This concludes the implementation of the Admin Edit Brand functionality.
