Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

How to get last insert id in Eloquent ORM laravel

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.

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()".

But I need to get

"Model::create($data)";

My Query:

GeneralSettingModel::create($GeneralData);

User::create($loginuserdata);

How can I retrieve the last inserted id?

Upvotes: 24

Views: 103831

Answers (11)

Mohsen
Mohsen

Reputation: 4235

This is an easier way that works on all of the ways that you inserted:

DB::getPdo()->lastInsertId()

Upvotes: 1

mercury
mercury

Reputation: 2735

$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.

Upvotes: 1

Md. Saidur Rahman Milon
Md. Saidur Rahman Milon

Reputation: 2911

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]']);

Upvotes: 2

MD Ashik
MD Ashik

Reputation: 9835

This code works with Laravel 5.3:

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

User pages/site I was call data.id

Upvotes: 1

horse
horse

Reputation: 725

This is my try:

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

And use $model_ins->id.

Upvotes: 2

Majbah Habib
Majbah Habib

Reputation: 8558

**** For Laravel ****

$user = new User();

$user->name = 'John';

$user->save();

//Getting Last inserted id

$insertedId = $user->id;

Upvotes: 22

Raham
Raham

Reputation: 4951

You may do it as,

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

}

Hope this will help you.

Upvotes: 2

Krzysiek Grzembski
Krzysiek Grzembski

Reputation: 994

As others said bfore me you may retrieve id by using

$model->id;

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

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

you may retrieve last inserted id by calling

$model->userid;

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

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.

Upvotes: 2

Pankaj katiyar
Pankaj katiyar

Reputation: 464

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

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

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

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

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

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

Upvotes: 6

user2339271
user2339271

Reputation: 96

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

Upvotes: 3

Edgar Orozco
Edgar Orozco

Reputation: 2782

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:"

$insertedId = $user->id;

So in your sample:

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

then on table2 it is going to be

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

Upvotes: 50

Related Questions