Tekin GÜNDOĞDU
Tekin GÜNDOĞDU

Reputation: 17

laravel 8 : insert method get last insert id in eloquent (İnsert Method)

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::insert($data)";

My Query:

$insert = Product::insert($alldata);

 if ($insert) {
                return response()->json([
                    'success' => 'Ürün başarıyla Eklendi.',
                    'id' => ???????
                ]);

            }

How can I retrieve the last inserted id?

Upvotes: 0

Views: 796

Answers (2)

Mudassar Abbas
Mudassar Abbas

Reputation: 176

You can Also Use insertGetId() function

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

Upvotes: 1

pr1nc3
pr1nc3

Reputation: 8338

$insert = Product::create($alldata);
$insert->id

Holds the id of the inserted item.

Basically you have the whole collection in your $insert variable, of the item that you inserted.

Upvotes: 1

Related Questions