How do I get all children that fall under a parent in eloquent?

前端 未结 6 2093
日久生厌
日久生厌 2021-01-04 21:45

In my database, I have a Categories table. Categories can have parent categories, making it a recursive relationship

I also have a products table. Each product falls

6条回答
  •  执笔经年
    2021-01-04 22:30

    please try the below Has Many Through relation and post the result

    class Category extends Model
    {
        public function products()
        {
            return $this->hasManyThrough(
                'App\Product', 'App\Category',
                'parent_id', 'catergory_id', 'id'
            );
        }
    }
    

    Then you can use $category->products; to find your products

提交回复
热议问题