Laravel get a collection of relationship items

后端 未结 4 1116
难免孤独
难免孤独 2021-01-07 18:01

I\'m stuck on this what seems like a simple task.

I have a User that has many Shops that have many Products..

I\'m trying to get all the Pr

4条回答
  •  清歌不尽
    2021-01-07 18:13

    what you need is a relationship between the User Model and the Product Model ..

    this is possible using hasManyThrough relationship ..

    USER MODEL

    public function products()
    {
        return $this->hasManyThrough('App\Product', 'App\Shop')
    }
    

    now, you can access all the user's products with

    Auth::user()->products;
    

提交回复
热议问题