2016-12-19 4 views
0

Ich habe dies:Laravel Art auf zusätzliches Feld

$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(); 

ich auch versucht habe: $companies->sortBy('distance')

und einige andere Wege, die nicht funktionierten.

Jeder hat eine Idee, wie dies zu erreichen ist? Dank

Antwort

0

Okay, ich es herausgefunden.

$sorted = $companies->sortBy('distance'); 
$sorted->values()->all(); 

$companies = $sorted; 

Ich denke, es braucht einen Puffer, wenn es sortiert.

0

versuchen, den folgenden Code:

$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); 

Weitere Informationen finden Sie unter http://php.net/manual/en/function.array-multisort.php

Verwandte Themen