'Get all the products which are inside parent Category and child categories in Laravel with the help of relation

I have category and product table as follows:

Categories:

id | name | slug | parent_id

Products :

id | brand_id | name | slug | price | desc

Product_categories :

id | category_id | product_id

Product Model:

<?php

namespace App\Models;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    /**
     * @var string
     */
    protected $table = 'products';

    /**
     * @var array
     */
    protected $fillable = [
        'brand_id', 'sku', 'name', 'slug', 'description', 'quantity',
        'weight', 'price', 'sale_price', 'status', 'featured',
    ];

    /**
     * @var array
     */
    protected $casts = [
        'quantity'  =>  'integer',
        'brand_id'  =>  'integer',
        'status'    =>  'boolean',
        'featured'  =>  'boolean'
    ];

     /**
     * @param $value
     */
    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value;
        $this->attributes['slug'] = Str::slug($value);
    }

    public function categories()
    {
    return $this->belongsToMany(Category::class, 'product_categories', 'product_id', 'category_id');
    }

    public function product_attributes()
    {
    return  $this->belongsToMany(Attribute::class, 'product_attributes')->withPivot('value', 'quantity');
    }

    public function users()
    {
    return $this->belongsToMany(User::class,'wishlists');
    }

}

Category Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use TypiCMS\NestableTrait;
use Illuminate\Support\Str;


class Category extends Model
{
    use NestableTrait;
    protected $table = 'categories';

    protected $fillable = [
        'name', 'slug', 'description', 'parent_id', 'featured', 'menu', 'image',
    ];

    protected $casts = [
        'parent_id' => 'integer',
        'featured' => 'boolean',
        'menu' => 'boolean',
    ];

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = $value;
        $this->attributes['slug'] = str_slug($value);
    }

    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }


    public function products()
    {
    return $this->belongsToMany(Product::class, 'product_categories', 'category_id', 'product_id');
    }
 }

I'm getting products related to specific category :

http://localhost:8000/category/kurta-sets

Now i'm getting all kurta sets, but i also want to get all products when i pass slug of parent category for eg: if i pass http://localhost:8000/category/top-wear then i should get all products which are in Shirts, Jackets, T-shirt.

enter image description here

I'm getting all child categories in my controller with following code:

public function show($slug)
    {
        $category = $this->categoryRepository->findBySlug($slug);
        $child_categories=$this->categoryRepository->getChildCategories($category->id); 
        dd($child_categories->children);  //return all child categories which are inside parent category
        return view('site.pages.category', compact('category','child_categories'));
    }


Solution 1:[1]

             $category_ids = $category->child->pluck('id');

            $products = Product::whereHas('categories', function ($query) use ($category_ids) {
                $query->whereIn('category_id', $category_ids);
            });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 furkankufrevi