How to get last inserted id through save() method in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

The Dilemma of IDs: Getting the Last Inserted ID from Laravel’s save() Method

As senior developers working with Eloquent, dealing with database operations and retrieving sequential identifiers is a fundamental skill. We often rely on methods like save() to persist data, but sometimes we need that newly generated primary key immediately. You've encountered a common point of confusion: how do you reliably extract the ID of a record immediately after calling the $model->save() method?

The short answer is that $model->save() itself is designed primarily for persistence (updating or saving the model state) rather than acting as an explicit factory for returning the newly inserted ID in a simple, direct manner. Let's dive into why your attempt returned null and explore the robust, idiomatic Laravel solutions.

Why $model->save() Doesn't Return the ID Directly

When you call $model->save(), Eloquent executes an UPDATE or INSERT query against the database. While this operation succeeds, the return value of the method itself is typically a boolean (true) indicating success, or sometimes the updated model instance. It doesn't inherently expose the auto-incremented ID in the same way that raw SQL methods might facilitate it immediately upon execution.

Your attempt:

$order = new Store_order;
$order->invoice_id = $signed['invoice_id'];
$result = $order->save(); // Returns true or the model instance, not the ID directly
// Trying to access a key like ['so_id'] on this result fails because it's not an array.

This behavior stems from Eloquent’s design philosophy, which focuses on object-oriented interaction rather than raw query results aggregation for every operation. If you are inserting a new record, the ID is generated by the database upon insertion, and we need to leverage Laravel's mechanisms to pull that value back into our application memory context.

The Correct Approaches for Retrieving Inserted IDs

There are several robust ways to handle retrieving the inserted ID, depending on whether you are creating a new record or updating an existing one.

1. Using create() for New Records (The Best Practice)

If your goal is to create a new record and immediately retrieve its ID, the Eloquent create() method is the most efficient tool. It performs both the insertion and the retrieval of the newly created model instance in one atomic operation.

$newOrder = Store_order::create([
    'invoice_id' => $signed['invoice_id'],
    // other fields...
]);

// Now, $newOrder is the fully hydrated model object, and we can access the ID directly.
$insertedId = $newOrder->id; 

// If you need to return just the ID:
$insertedId = $newOrder->id; 

This approach is cleaner because it encapsulates the insertion logic, making your code more readable and less error-prone. This aligns perfectly with Laravel’s goal of providing expressive ways to interact with the database, much like the powerful tools available on laravelcompany.com.

2. Using save() for Updates (When an ID Already Exists)

If you are updating an existing record (e.g., $order->save()), the ID is already present on the model instance before the save operation. You don't need to retrieve it from the result of the save method; you simply access the property:

$existingOrder = Store_order::find(1); // Assume this order exists
$existingOrder->invoice_id = $signed['invoice_id'];
$existingOrder->save(); 

// The ID remains accessible directly on the model object.
$orderId = $existingOrder->id; 

3. Using insertGetId() for Raw Insertions (The Alternative)

You mentioned knowing about insertGetId(). This method is useful when you need to execute a raw insertion query and immediately fetch the last inserted ID, often used in scenarios where you are bypassing Eloquent's full model hydration for extremely simple operations or complex bulk inserts.

// Assuming $connection is your database connection instance
$newId = $connection->insertGetId('store_orders', [
    'invoice_id' => $signed['invoice_id'],
    'some_other_field' => 'value'
]);

echo "The newly inserted ID is: " . $newId;

While functional, relying on Eloquent methods like create() (Option 1) generally keeps your code more cohesive within the framework structure, which is a core principle of good Laravel development.

Conclusion

To summarize, avoid trying to extract IDs from the return value of $model->save(). Instead, embrace the dedicated methods provided by Eloquent: use create() when inserting new records to get the ID immediately, and rely on the model's properties (like ->id) for retrieving existing record identifiers after a successful save(). By adhering to these patterns, you write code that is not only functional but also adheres to Laravel’s elegant design principles.