雄辩的sortBy-指定asc或desc

Lov*_*ock 4 php arrays eloquent laravel-4

尝试对我雄辩的收藏进行排序:

$collection->sortBy('field');
Run Code Online (Sandbox Code Playgroud)

Laravel 4的文档中没有有关如何为这种排序方法选择降序或升序的信息。

可能吗?

Rod*_*tti 8

ASC排序:Laravel文档-sortBy()

$collection->sortBy('field');

DESC排序:Laravel文档-sortByDesc()

$collection->sortByDesc('field');


Bis*_*del 7

Laravel 5

https://laravel.com/docs/5.8/collections#method-sortby

Laravel 4

https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_sortBy

用法

$collection->sortBy('field', [], true); // true for descending
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!从不知道那个网站。去了sortByDesc()。 (2认同)
  • `sortBy('field', [], true)` 返回 `arsort(): Argument #2 ($flags) 必须是 int 类型,在文件中给出的数组`错误!!! (2认同)

小智 7

4.2中,第二个参数需要是int,默认为SORT_REGULAR

https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_sortBy

$collection->sortBy('field', SORT_REGULAR, true); // 降序为真