In this video we are going to learn about Create Subcategories.
So lets see how can we Create Subcategories.
First of all lets create a model and migration for Subcateogries.
So switch to the command prompt and run the command.

php artisan make:model Subcategory –m

Now switch to the project and lets open the subcategory migration.
So go inside the database directory then migration and from here lets open create_subcategory_table_migration and the following columns.

<?php

use Illuminate\\Database\\Migrations\\Migration;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Support\\Facades\\Schema;

class CreateSubcategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('subcategories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->bigInteger('category_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('subcategories');
}
}

Here lets add some column.
Now lets migrate the migration.
So switch to the command prompt and run the command.

php artisan migrate

Now run the application.
So just write here.

php artisan serve

Now switch to the project and lets open the admin-add-category-component.blade.php view file and lets add here select control for parent category.


<div class=\"form-group\">
<label class=\"col-md-4 control-label\">Parent Category</label>
<div class=\"col-md-4\">
<select class=\"form-control input-md\" wire:model=\"category_id\">
<option value=\"\">None</option>
@foreach($categories as $category)
<option value=\"{{$category->id}}\">{{$category->name}}</option>
@endforeach
</select>
</div>
</div>


Now go to the AdminAddCategoryComponent.php class file and here inside the render method lets fetch all categories and pass to the component view file.


public function render()
{
$categories = Category::all();
return view('livewire.admin.admin-add-category-component',['categories'=>$categories])->layout('layouts.base');
}

Now inside the storeCategory method add here.

public function storeCategory()
{
$this->validate([
'name' => 'required',
'slug' => 'required|unique:categories'
]);

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


Now its done.
So lets check this.
So switch to browser and refresh the page.
Now lets add a category.
Enter the category name.
First create the parent category so keep this parent category none.
Now click on add category.
Alright, now lets add subcategory.
So enter there the subcategory name and choose the patent category.
Now click on add.
Add one more Sub category.
So write here category name and select subcategory.
Now lets check the database so go to the phpMyAdmin and open database now browser the category table and here you can see the parent category Electronics now browse the subcategories table.
and here you can see the subcategory mobile and laptop which parent category id is 3 which is id of electronics.
So in this way you can Create Subcategories.
In next tutorial we will see how can display here category with their subcategories.