1
특성에서 Laravel Eloquent와 더 깊은 관련 오브젝트를 통해 DB에서 성공적으로 가져 오기가 주어진 경우 - 하위 오브젝트 속성별로 어떻게 정렬합니까?Laravel에서 웅변 물체 모음을 어떻게 분류합니까?
특성에서 Laravel Eloquent와 더 깊은 관련 오브젝트를 통해 DB에서 성공적으로 가져 오기가 주어진 경우 - 하위 오브젝트 속성별로 어떻게 정렬합니까?Laravel에서 웅변 물체 모음을 어떻게 분류합니까?
$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;
});
당신은 접근 속성과 함께 sortBy
방법을 사용할 수 있습니다
class User extends Model
{
public function getNameAttribute()
{
return $this->first_name.' '.$this->last_name;
}
}
$users = MyModel::all()->sortBy('name');
안녕하세요 요셉, 귀하의 의견 주셔서 감사. 나는 단순한 비트가 아닌,보다 깊고 강력한 분류를 돕기 위해 모든 사람들을위한 게시물을 만들고자합니다. 그 노력에 대한 나의 질문을 편집하여 도울 수 있습니까? –