0

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?

  • This is something you could have found out using the docs, even in the getting started section. But ok, you've got your answer. Next time at least try to look inside the docs. – Gert B. Aug 20 '21 at 13:21
  • How do you know I haven't read the documentation????But I'm getting an error so I asked if I could find a different answer. – Tekin GÜNDOĞDU Aug 20 '21 at 13:56
  • How do I know. Because you don't use Eloquent models as the docs show. Inserting and retrieving models are the basics and are in the part of the ORM: https://laravel.com/docs/8.x/eloquent#inserts . – Gert B. Aug 20 '21 at 14:10

2 Answers2

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

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • if ($insert) { return response()->json([ 'success' => 'Ürün başarıyla Eklendi.', 'id' =>$insert->id ]); This way I can't send the Id to the blade page – Tekin GÜNDOĞDU Aug 20 '21 at 13:58
  • No this is your response. If you want to pass it to your view, you need to send it as a parameter using your View form. Your question is about an API response or at least this is what it looks like you are trying to achieve. – pr1nc3 Aug 20 '21 at 14:01
  • My Query : $insert = Product::insert($alldata); This is not my query: $insert = Product::create($alldata); ok? – Tekin GÜNDOĞDU Aug 20 '21 at 14:03
  • Do you have another solution based on the comment above? – Tekin GÜNDOĞDU Aug 20 '21 at 14:06
  • Use the query in my answer and access the id the way i mentioned. Your question was about retrieving the id of the element you created and put it in your json response and my answer is solely based on that. – pr1nc3 Aug 20 '21 at 14:10
  • I need to send this ID to the blade page as a response to the data received via ajax. How can I do it? – Tekin GÜNDOĞDU Aug 20 '21 at 14:11
  • If you are using ajax and make the call to the API of this controller then your id will be there inside your json. Just use my answer as is using create instead of insert. Just replace in your response the question marks with `$insert->id` – pr1nc3 Aug 20 '21 at 14:12
  • Unfortunately it didn't work. I can't send the ID to the blade. However, when I manually type the value as the ID, I can send it to the Blade. For example, I can send like this: return response()->json([ 'success' => 'Product Added Successfully.', 'id' =>7// Sending response ]); But return response()->json([ 'success' => 'Product Added Successfully.', 'id' =>$insert->id // Failed to send response ]); – Tekin GÜNDOĞDU Aug 20 '21 at 14:45
  • Is your column called id? what does your $insert value show? print_r it before the response and try to debug. What fields are in this collection? – pr1nc3 Aug 20 '21 at 14:50
  • Yeah. column name is "id".. My model file looks like this: protected $table = "products"; protected $primaryKey = 'id'; /** * @var string[] */ protected $fillable = [ 'id', 'ProductCode', ..................... ..................... ..................... ......................... – Tekin GÜNDOĞDU Aug 20 '21 at 14:58
1

You can Also Use insertGetId() function

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