Laravel 4和雄辩ORM - 如何选择表的最后5行 [英] Laravel 4 and Eloquent ORM - How to select the last 5 rows of a table

查看:89
本文介绍了Laravel 4和雄辩ORM - 如何选择表的最后5行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何选择表格的最后5行以在主屏幕上显示,ids中可能有空白。这个数据库有1000行,我不想每次我去我的应用程序的主屏幕时都要打电话给他们。问题是有时候由于各种原因有时会在数据库中删除行,例如,如果最后一行的id为4023,则第二行的id可能为4020,因此我不能仅仅使用长度和向后计数。理想情况下,它将像

  $ get_5_rows = DB :: table('rows') - > take(5) - > ;得到(); 

除了收集最后5个最后5行的前5行。

感谢您的帮助!任何和所有的帮助是非常感谢!

解决方案

您可以尝试这样:

  $ rows = DB :: table('rows') - > orderBy('id','desc') - > take(5) - > get() ; 

您还可以使用 orderBy('created_at')如果该字段可用。另一个类似的回答此处


I'm trying to figure out how to select the last 5 rows of a table to display on a home screen when there might be gaps in the ids. The database this is for has 1,000s of rows and I don't want to have to call all of them to take the last 5 every time I go to my app's home screen. The problem is that rows sometimes are deleted in the database for various reasons so, for example, if the last row's id is 4023 the second to last row's id might be 4020, so I can't just use the length and count backwards. Ideally it would work like

$get_5_rows = DB::table('rows')->take(5)->get();

Except that instead of collecting the first 5 rows that it would take the last 5.

Thank y'all so much for the help! Any and all help is greatly appreciated!

解决方案

You may try this:

$rows = DB::table('rows')->orderBy('id', 'desc')->take(5)->get();

You may also use orderBy('created_at') if that field is available. Another similar answer here.

这篇关于Laravel 4和雄辩ORM - 如何选择表的最后5行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆