php Eloquent sortBy - 指定 asc 或 desc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30649113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent sortBy - Specify asc or desc
提问by Lovelock
Trying sort my eloquent collection:
尝试整理我雄辩的收藏:
$collection->sortBy('field');
There is no information in Laravel 4's docs on how to choose descending or ascending for this sort method.
Laravel 4 的文档中没有关于如何为这种排序方法选择降序或升序的信息。
Is it possible?
是否可以?
回答by Rodrigo Pauletti
To sort by ASC: Laravel Docs - sortBy()
按ASC排序:Laravel Docs - sortBy()
$collection->sortBy('field');
$collection->sortBy('field');
To sort by DESC: Laravel Docs - sortByDesc()
按DESC排序:Laravel Docs - sortByDesc()
$collection->sortByDesc('field');
$collection->sortByDesc('field');
回答by Bishal Paudel
Laravel 5
Laravel 5
https://laravel.com/docs/5.8/collections#method-sortby
https://laravel.com/docs/5.8/collections#method-sortby
Laravel 4
Laravel 4
https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_sortBy
https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_sortBy
Usage
用法
$collection->sortBy('field', [], true); // true for descending
回答by WoodyNaDobhar
In 4.2, the second parameter needs to be an int, SORT_REGULAR is default
4.2中,第二个参数需要是int,默认SORT_REGULAR
https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_sortBy
https://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_sortBy
$collection->sortBy('field', SORT_REGULAR, true); //true for descending
$collection->sortBy('field', SORT_REGULAR, true); //降序为真