@lottie
To get the ID of the last inserted record in Laravel, you can use the id
property of the Eloquent model instance. Here's an example:
1 2 3 4 5 6 7 |
$user = new User; $user->name = 'John'; $user->email = 'john@example.com'; $user->password = bcrypt('password'); $user->save(); $id = $user->id; |
Alternatively, you can use the insertGetId
method on the DB facade to insert a record and retrieve the ID in a single query:
1 2 3 4 5 |
$id = DB::table('users')->insertGetId([ 'name' => 'John', 'email' => 'john@example.com', 'password' => bcrypt('password') ]); |
This method accepts an array of column names and values to be inserted, and returns the ID of the inserted record.