Laravel Eloquent: Ordering results of all()

后端 未结 10 1535
野的像风
野的像风 2020-12-07 10:09

I\'m stuck on a simple task. I just need to order results coming from this call

$results = Project::all();

Where Project is

10条回答
  •  再見小時候
    2020-12-07 10:55

    You could still use sortBy (at the collection level) instead of orderBy (at the query level) if you still want to use all() since it returns a collection of objects.

    Ascending Order

    $results = Project::all()->sortBy("name");
    

    Descending Order

    $results = Project::all()->sortByDesc("name");
    

    Check out the documentation about Collections for more details.

    https://laravel.com/docs/5.1/collections

提交回复
热议问题