我想在单个数组中获取特定列的所有值。寻找可以做到这一点的雄辩功能。
这样的事情:
Model::select('id')->where('type', 'user')->asArray()
预期结果是:
[1,2,3,4,5,6,7,8,9]
答案 0 :(得分:1)
Eloquent没有一个内置功能来执行此操作。但是,您可以使用map
将Model
个对象的集合展平为数组:
$coll = Model::select('id')->where('type', 'user')->get();
// Pull the id out of each member of the collection
$coll = $coll->map(function ($item, $key) {
return $item->id;
});
// Convert collection to an array
print_r($coll->toArray());