In this video we are going to learn about how to Edit Category.
So let see how can we edit the product category.
So first of all lets create a new livewire component for edit the category.
So switch to the command prompt and for creating the new livewire component run the command.

php artisan make:livewire admin/AdminEditCategoryComponent


Now switch to project and lets create the route for the AdminEditCategoryComponent.
So lets open web.php file and add the following route.

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


Now open AdminEditCategoryComponent.php Class File and add the following code.


<?php

namespace App\\Http\\Livewire\\Admin;

use App\\Models\\Category;
use Livewire\\Component;
use Illuminate\\Support\\Str;

class AdminEditCategoryComponent extends Component
{
public $category_slug;
public $category_id;
public $name;
public $slug;

public function mount($category_slug)
{
$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;
}

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

public function updated($fields)
{
$this->validateOnly($fields,[
'name' => 'required',
'slug' => 'required|unique:categories'
]);
}

public function updateCategory()
{
$this->validate([
'name' => 'required',
'slug' => 'required|unique:categories'
]);
$category = Category::find($this->category_id);
$category->name = $this->name;
$category->slug = $this->slug;
$category->save();
session()->flash('message','Category has been updated successfully!');
}

public function render()
{
return view('livewire.admin.admin-edit-category-component')->layout('layouts.base');
}
}


Now open the admin-category-component.blade.php view file add the following code.

<div>
<div class=\"container\" style=\"padding: 30px 0;\">
<div class=\"row\">
<div class=\"col-md-12\">
<div class=\"panel panel-default\">
<div class=\"panel-heading\">
<div class=\"row\">
<div class=\"col-md-6\">
Edit Category
</div>
<div class=\"col-md-6\">
<a href=\"{{route('admin.categories')}}\" class=\"btn btn-success pull-right\">All Category</a>
</div>
</div>
</div>
<div class=\"panel-body\">
@if(Session::has('message'))
<div class=\"alert alert-success\" role=\"alert\">{{Session::get('message')}}</div>
@endif
<form class=\"form-horizontal\" wire:submit.prevent=\"updateCategory\">
<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Category Name</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Category Name\" class=\"form-control input-md\" wire:model=\"name\" wire:keyup=\"generateslug\" />
@error('name') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Category Slug</label>
<div class=\"col-md-4\">
<input type=\"text\" placeholder=\"Category Slug\" class=\"form-control input-md\" wire:model=\"slug\" />
@error('slug') <p class=\"text-danger\">{{$message}}</p> @enderror
</div>
</div>

<div class=\"form-group\">
<label class=\"col-md-4 control-label\"></label>
<div class=\"col-md-4\">
<button type=\"submit\" class=\"btn btn-primary\">Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>


Now its done so lets check it.
switch to the browser and now edit any category.
Now change the category details and click on submit.
And here you can see the message category updated.
So in this way you update the product category so that's all about edit category.