In this video we are going to learn about Show Products by Subcategory.
So lets see how can we Show Products by Subcategory.
First of all lets add a new column in Products table.
For that lets create a migration.
So switch to the command prompt an run the command.

php artisan make:migration add_subcategory_id_to_products_table --table=products


Now switch to the project and lets open the migration.
So go inside the database directory then migration.
Now open add_subcategory_id_to_products_table this migration.
Now here lets add a column so write the following code.

<?php

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

class AddSubcategoryIdToProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->bigInteger('subcategory_id')->unsigned()->nullable();
$table->foreign('subcategory_id')->references('id')->on('subcategories')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropForeign('products_subcategory_id_foreign');
$table->dropColumn('subcategory_id');
});
}
}


Alright, now lets migrate this migration so switch to the command prompt and run the command.

php artisan migrate

Now run the application.


php artisan serve


Now switch to the project and lets open the Product model.
and here lets create a function here.

public function subCategories()
{
return $this->belongsTo(Subcategory::class,'subcategory_id');
}


Now lets open the web.php file and here.
Inside this product-category route lets add here one more optional parameter.


Route::get('/product-category/{category_slug}/{scategory_slug?}',CategoryComponent::class)->name('product.category');


Allright now go to the CategoryComponent.php class file.
Inside the mount method here lets add here one more argument and also create a property here as following.

public $scategory_slug;

public function mount($category_slug,$scategory_slug=null)
{
$this->sorting = \"default\";
$this->pagesize = 12;
$this->category_slug = $category_slug;
$this->scategory_slug = $scategory_slug;
}


Now inside the render method lets make the following changes.

public function render()
{
$category_id = null;
$category_name = \"\";
$filter = \"\";

if($this->scategory_slug)
{
$scategory = Subcategory::where('slug',$this->scategory_slug)->first();
$category_id = $scategory->id;
$category_name = $scategory->name;
$filter = \"sub\";

}
else{
$category = Category::where('slug',$this->category_slug)->first();
$category_id = $category->id;
$category_name = $category->name;
$filter = \"\";

}

if($this->sorting=='date')
{
$products = Product::where($filter.'category_id',$category_id)->orderBy('created_at','DESC')->paginate($this->pagesize);
}
else if($this->sorting==\"price\")
{
$products = Product::where($filter.'category_id',$category_id)->orderBy('regular_price','ASC')->paginate($this->pagesize);
}
else if($this->sorting==\"price-desc\")
{
$products = Product::where($filter.'category_id',$category_id)->orderBy('regular_price','DESC')->paginate($this->pagesize);
}
else{
$products = Product::where($filter.'category_id',$category_id)->paginate($this->pagesize);
}

$categories = Category::all();

return view('livewire.category-component',['products'=> $products,'categories'=>$categories,'category_name'=>$category_name])->layout(\"layouts.base\");
}




No go to the shop-component.blade.php view file and inside the subcategory add the following link inside the subcategories.

<a href=\"{{route('product.category',['category_slug'=>$category->slug,'scategory_slug'=>$scategory->slug])}}\" class=\"cat-link\"><i class=\"fa fa-caret-right\"></i> {{$scategory->name}}</a>


And also add the previous link on category-component.blade.php view file inside the subcategories link.
Now its done so lets check it.
So switch to the browser and refresh the page.
Now click on this category.
and here are the products of this category.
Now lets create some subcategory.
So login with admin and go to the category page and click on add.
Now enter the name Camera, Television and select the parent category vel magnam.
For now lets set the sub category_id manually.
So go to the phpMyAdmin and lets open the databse.
Now browse the product table now lets copy this product name find it.
Set here the sucategroy id.
and here lets edit any product and set the subcategory id.
Now save this.
Now go to the page and refresh the page.
Now you can see the product which is in this subcategory.
So in this way you can Show Products by Subcategory.
In next video we will see how can we add subcategory option on add product and edit product page in admin panel.