php Laravel 5 中的按行排序和限制结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29276065/
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
Order by row and limit result in Laravel 5
提问by Avi
I'm trying to get a result from the database in a laravel 5 based application and for the life of me can't figure it out.
我正在尝试从基于 laravel 5 的应用程序中的数据库中获取结果,但我终生无法弄清楚。
I want to chose the top 5 results DESC from a row called count. This is what I have:
我想从名为 count 的行中选择前 5 个结果 DESC。这就是我所拥有的:
$full = Fulls::all()->orderBy('count', 'desc')->take(5)->get();
I tried plenty of other ways too but nothing seems to work. Now I'm getting an error:
我也尝试了很多其他方法,但似乎没有任何效果。现在我收到一个错误:
FatalErrorException in indexController.php line 19: Call to undefined method Illuminate\Database\Eloquent\Collection::orderBy()
在 indexController.php 第 19 行中的 FatalErrorException:调用未定义的方法 Illuminate\Database\Eloquent\Collection::orderBy()
However, anywhere I look I see people working with orderBy()
, so... what am I doing wrong?
但是,无论我看哪里,我都会看到有人与之合作orderBy()
,所以......我做错了什么?
Thanks in advance...
提前致谢...
回答by David Kmenta
You should use Fulls::orderBy(..)->take(5)->get()
instead.
你应该Fulls::orderBy(..)->take(5)->get()
改用。
回答by Thomas Negash
If you want to sort/order a collection you can use the sortBy() method.
如果要对集合进行排序/排序,可以使用 sortBy() 方法。
eg.
例如。
$full = Fulls::get(); // Get the Fulls collections
$full = $full->sortBy('count')->take(5);
回答by nageen nayak
You can use skip
and take
function of laravel and orderBy
您可以使用skip
和take
laravel的功能和orderBy
Fulls::where([['user_id','=',auth()->user()->id]])
->where([['title','LIKE',"%".$text_val."%"]])
->orderBy('id','DESC')
->skip($offset)
->take(2)