[英]How to build this Laravel Eloquent Query
You can use some methods from collection :您可以使用集合中的一些方法:
$result = $data->groupBy(['market_id', 'base_currency', 'currency_id'])
->map(function ($item) {
return $item->map(function ($item) {
return $item->map(function ($item) {
return $item->map(function ($item) {
return $item['currency_id'];
});
})->flatten();
})->sortKeys();
});
dd($result->toArray());
array:1 [
1 => array:2 [
1 => array:2 [
0 => 2
1 => 3
]
2 => array:2 [
0 => 1
1 => 2
]
]
]
groupBy
The groupBy
method groups the collection's items by a given key. groupBy
方法按给定的键对集合的项目进行分组。 Multiple grouping criteria may be passed as an array.多个分组标准可以作为数组传递。 Each array element will be applied to the corresponding level within a multi-dimensional array:
每个数组元素将应用于多维数组中的相应级别:
$data->groupBy(['market_id', 'base_currency', 'currency_id']);
map
The map
method iterates through the collection and passes each value to the given callback. map
方法遍历集合并将每个值传递给给定的回调。
->map(function ($item) {
return $item;
});
flatten
The flatten
method flattens a multi-dimensional collection into a single dimension: flatten
方法将多维集合flatten
平为单维:
->flatten()
sortKeys
The sortKeys
method sorts the collection by the keys of the underlying associative array: sortKeys
方法按底层关联数组的键对集合进行排序:
->sortKeys()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.