Laravel E-Commerce Project - Admin Edit Subcategories
In this video, we will learn how to edit subcategories in the admin panel.
Let's explore how to achieve this. First, switch to the project and open the web.php file. Inside the edit category route, add an optional parameter:
Route::get('/admin/category/edit/{category_slug}/{scategory_slug?}',AdminEditCategoryComponent::class)->name('admin.editcategory');
Next, go to the AdminEditCategoryComponent.php class file and create two new properties:
public $scategory_slug;
public $scategory_id;
Inside the render method, fetch all categories and return them to the view:
public function render()
{
$categories = Category::all();
return view('livewire.admin.admin-edit-category-component',['categories'=>$categories])->layout('layouts.base');
}
Now, go to the admin-edit-category-component.php view file and add a select control after this:
<div class="form-group">
<label class="col-md-4 control-label">Parent Category</label>
<div class="col-md-4">
<select class="form-control" wire:model="category_id">
<option value="">None</option>
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
@error('slug') <p class="text-danger">{{$message}}</p> @enderror
</div>
</div>
Go back to the AdminEditCategoryComponent class file and inside the mount method, pass one more argument and set the default value to null. Make the following changes:
public function mount($category_slug,$scategory_slug=null)
{
if($scategory_slug)
{
$this->scategory_slug = $scategory_slug;
$scategory = Subcategory::where('slug',$scategory_slug)->first();
$this->scategory_id = $scategory->id;
$this->category_id = $scategory->category_id;
$this->name = $scategory->name;
$this->slug = $scategory->slug;
}
else
{
$this->category_slug = $category_slug;
$category = Category::where('slug',$category_slug)->first();
$this->category_id = $category->id;
$this->name = $category->name;
$this->slug = $category->slug;
}
}
Inside the updateCategory method, make the following changes:
public function updateCategory()
{
$this->validate([
'name' => 'required',
'slug' => 'required|unique:categories'
]);
if($this->scategory_id)
{
$scategory = Subcategory::find($this->scategory_id);
$scategory->name = $this->name;
$scategory->slug = $this->slug;
$scategory->category_id = $this->category_id;
$scategory->save();
}
else
{
$category = Category::find($this->category_id);
$category->name = $this->name;
$category->slug = $this->slug;
$category->save();
}
session()->flash('message','Category has been updated successfully!');
}
Now, go to the admin-categories-component.blade.php view file. Add an edit link after the subcategory, and write the following code:
<td>
<ul class="sclist">
@foreach($category->subCategories as $scategory)
<li><i class="fa fa-caret-right"></i> {{$scategory->name}}
<a href="{{route('admin.editcategory',['category_slug'=>$category->slug,'scategory_slug'=>$scategory->slug])}}" class="slink"><i class="fa fa-edit"></i></a>
</li>
@endforeach
</ul>
</td>
That's it! Let's test the functionality.
Switch to the browser and refresh the page. You will see the edit link. Click on it to edit the subcategory. Make the necessary changes and click on "Update". You will see the subcategory updated.
In this way, you can easily edit subcategories in the admin panel.