laravel 选择表格中的最后一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16549455/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:26:14  来源:igfitidea点击:

Select Last Row in the Table

laraveleloquent

提问by Carlos Su?ol

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.

我想检索插入到我的表中的最后一个文件。我知道该方法first()存在并为您提供表中的第一个文件,但我不知道如何获取最后一个插入。

回答by Joachim Isaksson

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;

您需要按照您现在正在订购的相同字段进行订购,但降序排列。举个例子,如果上传完成时你有一个时间戳upload_time,你会做这样的事情;

For Pre-Laravel 4

对于 Laravel 4 之前的版本

return DB::table('files')->order_by('upload_time', 'desc')->first();

For Laravel 4 and onwards

对于 Laravel 4 及更高版本

return DB::table('files')->orderBy('upload_time', 'desc')->first();

For Laravel 5.7 and onwards

对于 Laravel 5.7 及更高版本

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.

这将按上传时间、降序对 files 表中的行进行排序,并取第一个。这将是最新上传的文件。

回答by Nick

Use the latest scope provided by Laravel out of the box.

开箱即用地使用 Laravel 提供的最新范围。

Model::latest()->first();

That way you're not retrieving all the records. A nicer shortcut to orderBy.

这样你就不会检索所有记录。一个更好的 orderBy 快捷方式。

回答by Luis Milanese

You never mentioned whether you are using Eloquent, Laravel's default ORM or not. In case you are, let's say you want to get the latest entry of a User table, by created_at, you probably could do as follow:

你从来没有提到你是否在使用 Eloquent,Laravel 的默认 ORM。如果您是,假设您想通过 created_at 获取用户表的最新条目,您可能可以执行以下操作:

User::orderBy('created_at', 'desc')->first();

First it orders users by created_at field, descendingly, and then it takes the first record of the result.

首先它通过 created_at 字段对用户进行降序排序,然后获取结果的第一条记录。

That will return you an instance of the User object, not a collection. Of course, to make use of this alternative, you got to have an User model, extending Eloquent class. This may sound a bit confusing, but it's really easy to get started and ORM can be really helpful.

这将返回一个 User 对象的实例,而不是一个集合。当然,要使用这个替代方案,你必须有一个 User 模型,扩展 Eloquent 类。这听起来可能有点令人困惑,但它真的很容易上手,而且 ORM 真的很有帮助。

For more information, check out the official documentationwhich is pretty rich and well detailed.

有关更多信息,请查看非常丰富且详细的官方文档

回答by Haseena P A

To get last record details

获取最后一条记录的详细信息

  1. Model::all()->last();or
  2. Model::orderBy('id', 'desc')->first();
  1. Model::all()->last();或者
  2. Model::orderBy('id', 'desc')->first();

To get last record id

获取最后一条记录ID

  1. Model::all()->last()->id;or
  2. Model::orderBy('id', 'desc')->first()->id;
  1. Model::all()->last()->id;或者
  2. Model::orderBy('id', 'desc')->first()->id;

回答by mdamia

Laravel collections has method last

Laravel 集合最后有方法

Model::all() -> last(); // last element 
Model::all() -> last() -> pluck('name'); // extract value from name field. 

This is the best way to do it.

这是最好的方法。

回答by Mark-Hero

Try this :

尝试这个 :

Model::latest()->get();

回答by tiaguinhow

You can use the latest scope provided by Laravel with the field you would like to filter, let's say it'll be ordered by ID, then:

您可以将 Laravel 提供的最新范围与您要过滤的字段一起使用,假设它将按 ID 排序,然后:

Model::latest('id')->first();

So in this way, you can avoid ordering by created_atfield by default at Laravel.

因此,通过这种方式,您可以避免created_at在 Laravel 中默认按字段排序。

回答by Jimuel Palaca

To get the last record details, use the code below:

要获取最后一条记录的详细信息,请使用以下代码:

Model::where('field', 'value')->get()->last()

回答by Towelie

Another fancy way to do it in Laravel 6.x (Unsure but must work for 5.x aswell) :

在 Laravel 6.x 中执行此操作的另一种奇特方式(不确定,但也必须适用于 5.x):

DB::table('your_table')->get()->last();

DB::table('your_table')->get()->last();

You can access fields too :

您也可以访问字段:

DB::table('your_table')->get()->last()->id;

DB::table('your_table')->get()->last()->id;

回答by Dazzle

Model($where)->get()->last()->id

Model($where)->get()->last()->id