2016-07-10 3 views

답변

1
$stuff = MyModel::all(); 
$sortedStuff = $stuff->sort(function($a, $b) 
{ 
    $a = $a->getMyCalculatedAttribute(); 
    $b = $b->getMyCalculatedAttribute(); 
    //here you can do more complex comparisons 
    //when dealing with sub-objects and child models 
    if ($a->property === $b->property) { 
     return 0; 
    } 
    return ($a->property > $b->property) ? 1 : -1; 
}); 
1

당신은 접근 속성과 함께 sortBy 방법을 사용할 수 있습니다

class User extends Model 
{ 
    public function getNameAttribute() 
    { 
     return $this->first_name.' '.$this->last_name; 
    } 
} 

$users = MyModel::all()->sortBy('name'); 
+0

안녕하세요 요셉, 귀하의 의견 주셔서 감사. 나는 단순한 비트가 아닌,보다 깊고 강력한 분류를 돕기 위해 모든 사람들을위한 게시물을 만들고자합니다. 그 노력에 대한 나의 질문을 편집하여 도울 수 있습니까? –

관련 문제