Laravel 按附加字段排序
Laravel sort on additional field
我知道了:
$companies = Company::all();
foreach($companies as $company)
{
$company->distance = distance(); // a float type. My custom field.
}
$companies->sort(function ($a, $b){
return strcmp($a->distance, $b->distance);
})->values()->all();
我也尝试过:
$companies->sortBy('distance')
还有其他一些无效的方法。
有人知道如何完成这个吗?谢谢
尝试以下代码:
$companies = Company::all()->toArray();
foreach($companies as $key => $company)
{
$distance[$key] = $company["distance"] = distance();
}
//now do a multisort on your distance array
array_multisort($distance, SORT_ASC, $data);
有关详细信息,请参阅 http://php.net/manual/en/function.array-multisort.php
好的,我明白了。
$sorted = $companies->sortBy('distance');
$sorted->values()->all();
$companies = $sorted;
我猜它在排序时需要一个缓冲区。
我知道了:
$companies = Company::all();
foreach($companies as $company)
{
$company->distance = distance(); // a float type. My custom field.
}
$companies->sort(function ($a, $b){
return strcmp($a->distance, $b->distance);
})->values()->all();
我也尝试过:
$companies->sortBy('distance')
还有其他一些无效的方法。
有人知道如何完成这个吗?谢谢
尝试以下代码:
$companies = Company::all()->toArray();
foreach($companies as $key => $company)
{
$distance[$key] = $company["distance"] = distance();
}
//now do a multisort on your distance array
array_multisort($distance, SORT_ASC, $data);
有关详细信息,请参阅 http://php.net/manual/en/function.array-multisort.php
好的,我明白了。
$sorted = $companies->sortBy('distance');
$sorted->values()->all();
$companies = $sorted;
我猜它在排序时需要一个缓冲区。