Laravel Eloquent:对 all() 的结果进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17429427/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:31:52  来源:igfitidea点击:

Laravel Eloquent: Ordering results of all()

laravellaravel-4eloquentsql-order-by

提问by MatterGoal

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

我被困在一个简单的任务上。我只需要订购来自这个电话的结果

$results = Project::all();

Where Projectis a model. I've tried this

哪里Project有模型。我试过这个

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

But it didn't work. Which is the better way to obtain all data from a table and get them ordered?

但它没有用。从表中获取所有数据并对其进行排序的更好方法是什么?

回答by Travis B

You can actually do this within the query.

您实际上可以在查询中执行此操作。

$results = Project::orderBy('name')->get();

This will return all results with the proper order.

这将以正确的顺序返回所有结果。

回答by Yannick Y

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.

如果您仍然想使用 all(),您仍然可以使用 sortBy(在集合级别)而不是 orderBy(在查询级别),因为它返回一个对象集合。

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

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

回答by ken4ward

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

此外,为了支持前一个答案,也可以通过添加任何一个作为第二个参数以降序desc或升序对其进行排序asc

$results = Project::orderBy('created_at', 'desc')->get();

回答by Sebastien Horin

2017 update

2017年更新



Laravel 5.4 added orderByDesc() methods to query builder:

Laravel 5.4 向查询构建器添加了 orderByDesc() 方法:

$results = Project::orderByDesc('name')->get();

回答by DsRaj

While you need result for date as desc

虽然您需要日期作为 desc 的结果

$results = Project::latest('created_at')->get();

回答by doncadavona

DO THIS:

做这个:

$results = Project::orderBy('name')->get();

DON'T DO THIS:

不要这样做:

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

WHY?Briefly, the first approach is faster than the second approach.

为什么?简而言之,第一种方法比第二种方法快。

回答by Half Crazed

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

查看sortByEloquent的方法:http: //laravel.com/docs/eloquent

回答by hkcoyant

Note, you can do:

请注意,您可以这样做:

$results = Project::select('name')->orderBy('name')->get();

This generate a query like:

这会生成一个查询,如:

"SELECT name FROM proyect ORDER BY 'name' ASC"

In some apps when the DB is not optimized and the query is more complex, and you need prevent generate a ORDER BY in the finish SQL, you can do:

在一些应用中,当数据库没有优化,查询比较复杂,需要防止在结束SQL中生成ORDER BY时,可以这样做:

$result = Project::select('name')->get();
$result = $result->sortBy('name');
$result = $result->values()->all();

Now is php who order the result.

现在是 php 对结果进行排序。