【发布时间】:2016-03-10 12:28:28
【问题描述】:
我想在单个数组中获取特定列的所有值。寻找可以做到这一点的 Eloquent 函数。
类似这样的:
Model::select('id')->where('type', 'user')->asArray()
预期结果是:
[1,2,3,4,5,6,7,8,9]
【问题讨论】:
我想在单个数组中获取特定列的所有值。寻找可以做到这一点的 Eloquent 函数。
类似这样的:
Model::select('id')->where('type', 'user')->asArray()
预期结果是:
[1,2,3,4,5,6,7,8,9]
【问题讨论】:
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());
【讨论】: