Laravel选择数据库表上的最后一行 [英] Laravel select last row on database table
问题描述
我想获取表中插入的最后一个文件。我知道方法'第一'提供了表中的第一个文件,但我不知道如何做最后一个插入。
I want to get the last file inserted in the table. I know method 'first' that provides you the first file in the table but I don't know how to do to get the last insert.
Thx。
推荐答案
您需要按照现在订购的相同字段进行排序,但是会下降。
举个例子,如果你上传完成时,你有一个时间戳,叫做 upload_time
,你会做这样的事情;
You'll need to order by the same field you're ordering by now, but descending.
As an example, if you have a time stamp when the upload was done called upload_time
, you'd do something like this;
对于Pre-Laravel 4
For Pre-Laravel 4
return DB::table('files')->order_by('upload_time', 'desc')->first();
对于Laravel 4及以上
For Laravel 4 and onwards
return DB::table('files')->orderBy('upload_time', 'desc')->first();
这将按照上传时间排序文件表中的行,降序,拿第一个。这将是最新上传的文件。
This will order the rows in the files table by upload time, descending order, and take the first one. This will be the latest uploaded file.
这篇关于Laravel选择数据库表上的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!