选择表格中的最后一行 [英] Select Last Row in the Table
问题描述
我想检索插入到我的表中的最后一个文件.我知道方法 first()
存在并为您提供表中的第一个文件,但我不知道如何获取最后一个插入.
I would like to retrieve the last file inserted into my table. I know that the method first()
exists and provides you with the first file in the table but I don't know how to get the last insert.
推荐答案
您需要按现在排序的同一个字段排序,但降序.例如,如果您在上传完成时有一个名为 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;
对于 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();
适用于 Laravel 5.7 及更高版本
For Laravel 5.7 and onwards
return DB::table('files')->latest('upload_time')->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.
这篇关于选择表格中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!