在 Laravel 中查询来自模型 with() 的多个关系
Query More than one relationship from a model with() in Laravel
我需要帮助来查询基于关系的模型:
我有一个名为StoreStock
的模型,这个模型与一个Product
模型相关,它有两个模型关系,MasterList
和Price
。
我想获得所有 storeStock
与两个 Product 关系。
我知道我可以做类似的事情
StoreStock::all()->with('product.price')
->get()
有了这个,我只能选择价格或主列表
将关系 array
传递给 with
方法
StoreStock::with(['product.price','product.masterlist']) ->get()
这里稍微解释一下,很多Laravel方法支持字符串,也支持数组。您可以将鼠标悬停在特定方法上并获取智能感知。在你的情况下,它可以写成:
StoreStock::with(['product.price','product.masterlist'])->get()
如果你想运行对任何特定的关系进行特定的操作,你也可以这样写:
StoreStock::with(['product.price' => function(Builder $query) {
// your action required with the query
},'product.masterlist']) ->get()
希望有人觉得这有帮助
我需要帮助来查询基于关系的模型:
我有一个名为StoreStock
的模型,这个模型与一个Product
模型相关,它有两个模型关系,MasterList
和Price
。
我想获得所有 storeStock
与两个 Product 关系。
我知道我可以做类似的事情
StoreStock::all()->with('product.price')
->get()
有了这个,我只能选择价格或主列表
将关系 array
传递给 with
方法
StoreStock::with(['product.price','product.masterlist']) ->get()
这里稍微解释一下,很多Laravel方法支持字符串,也支持数组。您可以将鼠标悬停在特定方法上并获取智能感知。在你的情况下,它可以写成:
StoreStock::with(['product.price','product.masterlist'])->get()
如果你想运行对任何特定的关系进行特定的操作,你也可以这样写:
StoreStock::with(['product.price' => function(Builder $query) {
// your action required with the query
},'product.masterlist']) ->get()
希望有人觉得这有帮助