Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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
如何通过laravel集合从数组中获取属性?_Laravel_Collections_Eloquent - Fatal编程技术网

如何通过laravel集合从数组中获取属性?

如何通过laravel集合从数组中获取属性?,laravel,collections,eloquent,Laravel,Collections,Eloquent,我得到了一个与产品有多对多关系的订单对象: public function products() { return $this->belongsToMany('App\Product', 'order_products') ->withPivot('count', 'price'); } 我接受一个产品阵列的请求,然后将其附加到订单中。但如果我得到的是一个数组,而不是集合,那么我应该如何正确地执行此操作?我可以通过$requ

我得到了一个与产品有多对多关系的订单对象:

public function products() {
       return $this->belongsToMany('App\Product', 'order_products')
                   ->withPivot('count', 'price');
   }
我接受一个产品阵列的请求,然后将其附加到订单中。但如果我得到的是一个数组,而不是集合,那么我应该如何正确地执行此操作?我可以通过
$request->products
中的循环来完成,但这对我来说似乎是错误的

确切地说,我有一个数组:

$products = [
  ['id' => 4, 'count' => 5],
  ['id'=>5, 'count' => 3]        
];

如何在没有任何循环的情况下执行
$order->products->attach()
attach
detach
都接受ID数组:

$ids = collect($request->products)->pluck('id');

$order->products()->attach($ids);

另外
light\Support\Arr::pull
array\u pull
完整性:)