Laravel 8 Project Home Services - Admin Edit Service Category

In this video, we will learn about editing a service category. Let's see how we can edit a service category.

First of all, let's create a new Livewire component for editing a service category.

In the command prompt, execute the following command to create a new Livewire component:


php artisan make:livewire admin/AdminEditServiceCategoryComponent

Now, let's create the route for the AdminEditServiceCategoryComponent.

Go inside the routes directory, then open the web.php file, and add the route inside the AdminMiddleware route group:


Route::get('/admin/service-category/edit/{category_slug}',AdminEditServiceCategoryComponent::class)->name('admin.edit_service_category');

Now, open the AdminEditServiceCategoryComponent Class File and first of all, add the layout:

->layout('layouts.base');

Now, open the AdminAddServiceCategoryComponent View File, select all the text, and paste it inside the AdminEditServiceCategoryComponent View File.

Now, go to the AdminEditServiceCategoryComponent Class File and let's create some properties:


public $category_id;
public $name;
public $slug;
Public $image;
Public $newimage;

Now, go to the view file and change the property name to "Newimage" and also change it here.

Now, inside the class file, add the mount lifecycle hook method:


public function mount($category_id)
{
$category = Category::find($category_id);
$this->category_id = $category->id;
$this->name = $category->name;
$this->slug = $category->slug;
}

Now, add the function for generating a slug:


public function generateslug()
{
$this->slug = Str::slug($this->name,'-');
}

And now, create a function for updating a category:

Now, call this function on edit form submit. So, go to the component view file and add the function name "updateCategory".

Alright, now open the AdminServiceCategoryComponent view file and inside this table, let's add the edit category link here:


<a href="{{route('admin.edit_service_category',['category_id'=>$scategory->id])}}"><i class="fa fa-edit fa-2x text-info"></i></a>

Now, it's done. Let's check it.

Switch to the browser and now edit any category. Let's edit this category.

Now, change the category name and click on submit.

And here, you can see the message "Category updated".

Now, click on this link and here, you can see the category has been updated.

So, in this way, you can edit a service category.