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

时间:2022-10-25 08:33:40

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)";

My Query:

GeneralSettingModel::create($GeneralData);

User::create($loginuserdata);

How can I retrieve the last inserted id?

如何检索最后插入的ID?

9 个解决方案

#1


38  

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.

“您也可以使用create方法将新模型保存在一行中。插入的模型实例将从方法返回给您。但是,在此之前,您需要指定一个可填充或保护的属性。模型,因为所有Eloquent模型都可以防止质量分配。

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

保存或创建使用自动递增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);

#2


20  

**** For Laravel ****

****对于Laravel ****

$user = new User();

$user->name = 'John';

$user->save();

//Getting Last inserted id

$insertedId = $user->id;

#3


3  

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

#4


3  

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.

如果您正在使用DBfaçade,您可以将lastInsertID()方法附加到查询中。

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

#5


2  

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:

但只有当您使用标准命名约定才能发表雄辩。如果要为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-conventions where 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.

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

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假定主键是递增的整数值,这意味着默认情况下主键将自动转换为int。如果要使用非递增或非数字主键,则必须将模型上的public $ incrementing属性设置为false。

#6


2  

You may do it as,

你可以这样做,

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

}

Hope this will help you.

希望这会帮助你。

#7


0  

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

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

That`s my way to do that , I return the inserted object , then get its id or any other property ..

这是我的方法,我返回插入的对象,然后获取其ID或任何其他属性..

easy .. and complete

容易..而且完整

#8


0  

This code are working great for me: Laravel 5.3

这段代码对我很有用:Laravel 5.3

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

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

User pages/site i was call data.id

用户页面/网站我被称为data.id

#9


0  

Eloquent Model

$user = new Reports();        
$user->email= 'david@example.com';  
$user->save();
$lastInsertId = $user->id;

Using Query Builder

使用查询生成器

 $lastInsertId = DB::table('reports')->insertGetId(['email' => 'david@example.com']);

#1


38  

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.

“您也可以使用create方法将新模型保存在一行中。插入的模型实例将从方法返回给您。但是,在此之前,您需要指定一个可填充或保护的属性。模型,因为所有Eloquent模型都可以防止质量分配。

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

保存或创建使用自动递增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);

#2


20  

**** For Laravel ****

****对于Laravel ****

$user = new User();

$user->name = 'John';

$user->save();

//Getting Last inserted id

$insertedId = $user->id;

#3


3  

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

#4


3  

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.

如果您正在使用DBfaçade,您可以将lastInsertID()方法附加到查询中。

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

#5


2  

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:

但只有当您使用标准命名约定才能发表雄辩。如果要为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-conventions where 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.

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

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假定主键是递增的整数值,这意味着默认情况下主键将自动转换为int。如果要使用非递增或非数字主键,则必须将模型上的public $ incrementing属性设置为false。

#6


2  

You may do it as,

你可以这样做,

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

}

Hope this will help you.

希望这会帮助你。

#7


0  

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

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

That`s my way to do that , I return the inserted object , then get its id or any other property ..

这是我的方法,我返回插入的对象,然后获取其ID或任何其他属性..

easy .. and complete

容易..而且完整

#8


0  

This code are working great for me: Laravel 5.3

这段代码对我很有用:Laravel 5.3

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

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

User pages/site i was call data.id

用户页面/网站我被称为data.id

#9


0  

Eloquent Model

$user = new Reports();        
$user->email= 'david@example.com';  
$user->save();
$lastInsertId = $user->id;

Using Query Builder

使用查询生成器

 $lastInsertId = DB::table('reports')->insertGetId(['email' => 'david@example.com']);