Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将多个对象传递给laravel雄辩模型_Php_Database_Laravel_Eloquent - Fatal编程技术网

Php 如何将多个对象传递给laravel雄辩模型

Php 如何将多个对象传递给laravel雄辩模型,php,database,laravel,eloquent,Php,Database,Laravel,Eloquent,$stores有多个对象如何将多个对象传递给另一个查询,如下所示。需要匹配从Store模型返回的所有id。使用first()只返回一个对象,但在本例中,需要获取所有与这些商店相关的商店和产品 您可以使用where()实现此目的。以您的示例为基础: $stores = $store->where('vendor_id', '=', $id)->get(); $products = Product::where('store_id', '=', $stores->id)->

$stores
有多个对象如何将多个对象传递给另一个查询,如下所示。需要匹配从
Store
模型返回的所有
id
。使用
first()
只返回一个对象,但在本例中,需要获取所有与这些商店相关的商店和产品

您可以使用
where()
实现此目的。以您的示例为基础:

$stores = $store->where('vendor_id', '=', $id)->get(); 
$products = Product::where('store_id', '=', $stores->id)->get();

我想您需要这样的东西,
$id
是一个给定的参数或变量:

$stores = $store::where('vendor_id', $id)->lists('id');
$products = Product::whereIn('store_id', $stores)->get();
这是假设您正在使用不同型号的默认表名,即
产品
型号的表名

方法
列出($column\u name)
将返回一个包含给定列元素的数组。
where($array)
子句的作用类似于SQL中的
ÌN
子句


注意:当使用
where
时,您不必将
=
符号放在此处,因为如果您将其留空,Laravel将对其进行解释。

假设您已经创建了模型关系,您可以这样做

$stores_id = Store::all()->where('vendor_id', $id)->lists('id');
$products = DB::table('products')->whereIn('id', $stores_id)->get();
查询将获取所选商店的所有产品

$stores = $store -> with('products') -> where('vendor_id',$id) ->first();