In this video we are going to learn about Edit Subcategories.
So lets see how can we Edit Subcategories.
Switch to the project and lets open the web.php file and here.
Inside the edit category route.
Lets add a optional parameter.


Route::get('/admin/category/edit/{category_slug}/{scategory_slug?}',AdminEditCategoryComponent::class)->name('admin.editcategory');


Now go to the AdminEditCategoryComponent.php class file and create two new properties.

public $scategory_slug;
public $scategory_id;


Now inside the render method lets fetch all categories and return to 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 here lets add after this a select control

<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>



Now go to AdminEditcategorycomponent class file and here inside the mount method here pass one more argument and set the default value null.
And make changes as following.

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;
}
}


Now inside the updateCategory method here 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.
After subcategory add edit link so write here.

<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>


Now its done so lets check it.
So switch to browser and refresh the page.
Now you can see here the edit link.
Now lets edit this subcategory.
Now click on update and you see here subcategory updated.
So in this way you can Edit Subcategories.