How to get last insert id in Eloquent ORM laravel

How to get last insert id in Eloquent ORM laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving IDs After create() in Eloquent

When using Model::create($data), you often need to capture the primary key of the newly inserted row immediately. While $model->save() returns the model instance (which contains the ID if auto-incrementing), create() is designed to be more explicit: it saves the model and then returns the primary key of the inserted record. This behavior was introduced in Laravel 4.x and refined in later versions, but understanding exactly how it works prevents common pitfalls when handling multiple inserts or custom keys.

If you are inserting a single row like this:

$userId = User::create($loginuserdata);

The $userId variable will hold the primary key of the newly created User. This is convenient because you don't need to access the model instance again to get the ID. However, this convenience comes with a specific constraint: create() returns the raw value of the primary key column from the database, not the entire model object. If your primary key is an integer, you get an integer; if it's a string (e.g., UUIDs), you get a string. Attempting to call methods like $userId->name will fail because $userId is just a scalar value in this context.

Handling Multiple Inserts

Your source material shows two distinct create() calls in sequence:

GeneralSettingModel::create($GeneralData);
User::create($loginuserdata);

In this scenario, you get the ID of the last inserted row only by capturing the return value of the second call. The first call (GeneralSettingModel) returns its own primary key, which is discarded unless you assign it to a variable. If your logic depends on the order of insertion or requires IDs from both models simultaneously, you must explicitly capture both:

$settingId = GeneralSettingModel::create($GeneralData);
$userId    = User::create($loginuserdata);

// Now you have both IDs available for further processing

The "Last Insert ID" Misconception

The term "last insert id" can be misleading in the context of Eloquent. In raw SQL, LAST_INSERT_ID() is a session-specific function that returns the most recent auto-generated key inserted into any table within the current connection session. However, Model::create() does not call this function directly; instead, it relies on the database adapter to return the generated value after the insert transaction completes.

If you are using MySQL, PostgreSQL, or SQLite with their respective PDO drivers, the underlying driver handles retrieving the auto-increment value and passes it back to Laravel. This means that even if you insert into multiple tables within a single script execution, create() only returns the ID of the row it just created, not a global counter across all tables in the application.

Trade-offs with Custom Keys

If your primary key is not an auto-incrementing integer—for example, if you are using UUIDs or custom string identifiers—create() will return whatever value was assigned to that column in the $data array. In such cases, capturing the return value is trivial because it's simply the ID you just passed in (or a generated UUID). However, if you have disabled auto-increment and rely on explicit values, ensure your database configuration allows for unique constraint violations to be handled gracefully, as create() does not automatically roll back on constraint failure unless wrapped in a transaction.

When Not to Use create()

While Model::create() is syntactically cleaner than $model = new Model($data); $model->save();, it can obscure where the ID comes from if you are debugging complex batch operations. Some developers prefer using insertOrGet or manual instantiation with save() when they need to manipulate the model instance before saving, as create() returns only the ID and not the full hydrated model instance immediately after insertion.

For high-volume inserts or scenarios requiring detailed error reporting on each row, consider using bulk insert methods like Model::insert($data) which return an array of affected rows. In those cases, you cannot use create() to get IDs in a loop efficiently.

If you need deeper insights into the ORM's behavior regarding ID generation or transaction handling, resources from LaravelCompany can provide additional context on best practices for database operations within the framework.

Our network