php 如何在 Eloquent ORM laravel 中获取最后一个插入 ID

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

How to get last insert id in Eloquent ORM laravel

phplaraveleloquent

提问by Deenadhayalan Manoharan

I am performing a database operation with "Eloquent ORM in Laravel". I just want to take the last insert id (not the maximum id) in the database.

我正在使用“Laravel 中的 Eloquent ORM”执行数据库操作。我只想获取数据库中的最后一个插入 id(不是最大 id)。

I searched to get last insert id in laravel Eloquent ORM, I got following link (Laravel, get last insert id using Eloquent) that is refer to get last insert id from following function "$data->save()".

我搜索以获取 Laravel Eloquent ORM 中的最后一个插入 id,我得到了以下链接(Laravel,使用 Eloquent 获取最后一个插入 id),它是指从以下函数“ $data->save()”中获取最后一个插入 id 。

But I need to get

但我需要得到

"Model::create($data)";

Model::create($data)”;

My Query:

我的查询:

GeneralSettingModel::create($GeneralData);

User::create($loginuserdata);

How can I retrieve the last inserted id?

如何检索最后插入的 ID?

回答by Edgar Orozco

Like the docs say: Insert, update, delete

就像文档说的:插入、更新、删除

"You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.

After saving or creating a new model that uses auto-incrementing IDs, you may retrieve the ID by accessing the object's id attribute:"

“您也可以使用 create 方法将新模型保存在一行中。插入的模型实例将从该方法返回给您。但是,在此之前,您需要在模型,因为所有 Eloquent 模型都可以防止大规模分配。

保存或创建使用自动递增 ID 的新模型后,您可以通过访问对象的 id 属性来检索 ID:"

$insertedId = $user->id;

So in your sample:

所以在你的样本中:

$user = User::create($loginuserdata);
$insertedId = $user->id;

then on table2 it is going to be

然后在 table2 上它将是

$input['table2_id'] = $insertedId;
table2::create($input);

回答by Majbah Habib

**** For Laravel ****

**** 对于 Laravel ****

$user = new User();

$user->name = 'John';

$user->save();

//Getting Last inserted id

$insertedId = $user->id;

回答by user2339271

$lastId = User::create($loginuserdata)->id;

回答by Pankaj katiyar

You could wrap your data in the insertGetId() method like this

您可以像这样将数据包装在 insertGetId() 方法中

$id = DB::table('table')->insertGetId( $data );

In this case the array $data would look something like this

在这种情况下,数组 $data 看起来像这样

$data = [ 'field' => 'data' , 'field' => 'data' ];

and if you're using the DB fa?ade you could just append the lastInsertID() method to your query.

如果您使用的是 DB fa?ade,您可以将 lastInsertID() 方法附加到您的查询中。

$lastInsertedID = DB::table('table')->insert( $data )->lastInsertId();

回答by Raham

You may do it as,

你可以这样做,

public function store(Request $request,ModelName $obj)
{
        $lastInsertedId = $obj->create($request->all())->id;

}

Hope this will help you.

希望这会帮助你。

回答by Krzysiek Grzembski

As others said bfore me you may retrieve id by using

正如其他人在我之前所说的那样,您可以通过使用来检索 id

$model->id;

but only if you are using standard naming convention for eloquent. If you want to use other name for primaryKey column, eg:

但前提是您使用标准命名约定来进行 eloquent。如果您想为 primaryKey 列使用其他名称,例如:

class Users extends Model{
    $primaryKey = 'userid';
}

you may retrieve last inserted id by calling

您可以通过调用检索最后插入的 id

$model->userid;

It is described in: https://laravel.com/docs/master/eloquent#eloquent-model-conventionswhere one can find:

它在:https: //laravel.com/docs/master/eloquent#eloquent-model-conventions中有描述,可以在其中找到:

Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.

In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.

Eloquent 还会假设每个表都有一个名为 id 的主键列。你可以定义一个 $primaryKey 属性来覆盖这个约定。

此外,Eloquent 假设主键是一个递增的整数值,这意味着默认情况下主键会自动转换为 int。如果您希望使用非递增或非数字主键,则必须将模型上的公共 $incrementing 属性设置为 false。

回答by Hos Mercury

$user = (new user)->create($request->all()) ;

        \Session::flash('message', 'Thanks , Your record No (' .$user->id . ') has been Successfully added');

This is my solution. I return the inserted object and get its ID or any other property.

这是我的解决方案。我返回插入的对象并获取其 ID 或任何其他属性。

回答by MD Ashik

This code works with Laravel 5.3:

此代码适用于 Laravel 5.3:

$upstatus = User::create($status);
return response()->json($upstatus);

User pages/site I was call data.id

我被调用的用户页面/站点 data.id

回答by srmilon

This is an eloquent model:

这是一个雄辩的模型:

$user = new Reports();        
$user->email= '[email protected]';  
$user->save();
$lastInsertId = $user->id;

A solution using Query Builder:

使用查询生成器的解决方案:

 $lastInsertId = DB::table('reports')->insertGetId(['email' => '[email protected]']);

回答by horse

This is my try:

这是我的尝试:

$model_ins = Model::orderBy('id', 'desc')->take(1)->first();

And use $model_ins->id.

并使用$model_ins->id.