Laravel获得了一系列关系项目 [英] Laravel get a collection of relationship items
本文介绍了Laravel获得了一系列关系项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我坚持这看似简单的任务.
I'm stuck on this what seems like a simple task.
我有一个拥有许多商店和许多产品的用户.
我正在尝试为某个用户获得所有产品.
I'm trying to get all the Products for a certain User.
此方法有效,并且正在将产品及其产品退还给商店
This is working, and is returning the Shops with their Products
\Auth::user()->shops()->with('products')->get();
但是我只需要一个产品集合.我尝试了以下操作,但它弄乱了查询
But I need a Collection of only the Products. I tried the following but it's messing up the Query
\Auth::user()->shops()->with('products')->select('products.*')->get();
知道我在做什么错吗?谢谢!
Any idea what I'm doing wrong? Thank you!
推荐答案
您可以使用:
\Auth::user()->shops()->with('products')->get()->pluck('products')->flatten();
如果不想复制,可以使用->unique()
if you don't want replicate, you can use ->unique()
这篇关于Laravel获得了一系列关系项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文