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

How to get last insert id in Eloquent ORM laravel

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

我在 laravel Eloquent ORM 中搜索获取最后一个插入 ID,我得到了以下 link (Laravel, get last insert id using Eloquent),这是指从以下获取最后一个插入 ID功能“$data->save()”。

但我需要

"Model::create($data)";

我的查询:

GeneralSettingModel::create($GeneralData);

User::create($loginuserdata);

如何检索最后插入的 ID?

如文档所说: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:"

$insertedId = $user->id;

因此在您的示例中:

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

然后在 table2 上它将是

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

**** 对于 Laravel ****

$user = new User();

$user->name = 'John';

$user->save();

//Getting Last inserted id

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

正如其他人在我之前所说的,您可以使用

检索 ID
$model->id;

但前提是您使用 eloquent 的标准命名约定。如果要为主键列使用其他名称,例如:

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

您可以通过调用

来检索最后插入的 ID
$model->userid;

描述于: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.

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

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

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

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

如果您使用的是数据库外观,则只需将 lastInsertID() 方法附加到您的查询即可。

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

你可以这样做,

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

}

希望对您有所帮助。

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

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

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

此代码适用于 Laravel 5.3:

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

用户pages/site我打电话给data.id

这是一个 eloquent 模型:

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

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

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

这是我的尝试:

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

并使用$model_ins->id.

这是一种更简单的方法,适用于您插入的所有方式:

DB::getPdo()->lastInsertId()