选择表中的最后一行 [英] Select Last Row in the Table

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

问题描述

我想检索插入到表中的最后一个文件.我知道方法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屋!

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