In this video we are going to learn about how to Add New Category by Admin.
So let see how can we add new category.
So first of all lets create new livewire component for adding new category.
Switch to command prompt and for creating new livewire component run the command

php artisan make:livewire admin/AdminAddCategoryComponent


Now switch to project and lets create the route for the AdminAddCategoryComponent.
So open web.php and inside the admin route group add the following route.


Route::get('/admin/category/add',AdminAddCategoryComponent::class)->name('admin.addcategory');


Now open the admin-add-category-component.blade.php view file write 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\">
Add New 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=\"storeCategory\">
<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\">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>


Now open the AdminAddCategoryComponent.php class file and write the following code.

<?php

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

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

class AdminAddCategoryComponent extends Component
{
public $name;
public $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 storeCategory()
{
$this->validate([
'name' => 'required',
'slug' => 'required|unique:categories'
]);

$category = new Category();
$category->name = $this->name;
$category->slug = $this->slug;
$category->save();
session()->flash('message','Category has been created successfully!');
}

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


Now open the AdminCategoryCompoent View file and add here the link for the adding new category inside this header.

<div class=\"panel-heading\">
<div class=\"row\">
<div class=\"col-md-6\">
All Categories
</div>
<div class=\"col-md-6\">
<a href=\"{{route('admin.addcategory')}}\" class=\"btn btn-success pull-right\">Add New</a>
</div>
</div>
</div>



Now before the form display the message

@if(Session::has('message'))
<div class=\"alert alert-success\" role=\"alert\">{{Session::get('message')}}</div>
@endif


Now its done so lets check it.
So switch to browser and refresh the page.
Now in Admin Categories Page for adding the new category click on Add New button.
After this you will see the add new category form.
Now enter the category name.
And here you can see slug is auto generated.
Now click on submit.
And you can see the message category has been added successfully.
Now click on all categories link for seeing all category and here you can see the newly created categories.
So in this way you can create new product category.