Laravel Eloquent: Ordering results of all()

后端 未结 10 1501
野的像风
野的像风 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:57

    In addition, just to buttress the former answers, it could be sorted as well either in descending desc or ascending asc orders by adding either as the second parameter.

    $results = Project::orderBy('created_at', 'desc')->get();
    
    0 讨论(0)
  • 2020-12-07 10:59

    Try this:

    $categories     =   Category::all()->sortByDesc("created_at");
    
    0 讨论(0)
  • 2020-12-07 11:00

    Check out the sortBy method for Eloquent: http://laravel.com/docs/eloquent

    0 讨论(0)
  • 2020-12-07 11:02

    2017 update


    Laravel 5.4 added orderByDesc() methods to query builder:

    $results = Project::orderByDesc('name')->get();
    
    0 讨论(0)
提交回复
热议问题