Support the ongoing development of Laravel.io →
posted 4 years ago
Laravel
Last updated 2 years ago.
0

Model::orderBy('id', 'desc')->first(); ?

Are you doing in a loop ?

0

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

0

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;
0

User::max('id') this is the easiest way... because last inserted ID will be the max always...

0

You can also More Infromation and get Working Source code : Fix Issue for switch case in laravel

Last updated 4 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

pakainfo pakainfo Joined 3 Apr 2020

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.