You can get last inserted id if the table has auto-increment id.
Something like this
$id = User::insertGetId([
'name' => 'User 2',
// ....
]);
Refer to the docs: https://laravel.com/docs/6.x/queries#inserts section Auto-Incrementing IDs
#cmiiw
If the data already inserted to database, i usually use:
$lastId = User::latest()->first()->id;
// or specify column name
$lastId = User::latest('user_id')->first()->user_id;
User::max('id') this is the easiest way... because last inserted ID will be the max always...
You can also More Infromation and get Working Source code : Fix Issue for switch case in laravel
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community