I am looking for something like :
DB::table('users')->getNextGeneratedId();
not
$user->save($data) $getNextGeneratedId = $user->id;
Does anybody know how to achieve this?
so, here we have find 4 diffrent way to get last inserted ID in laravel. you can use it and get last ID of inserted record in laravel application. DB::table('users')->insert([ 'name' => 'TestName' ]); $id = DB::getPdo()->lastInsertId();; dd($id); 3) Using create() method.
This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function. Think of it like this: when you don't exactly know what a query will return then you need to use get() .
Using Laravel Eloquent you can get one row using first() method, it returns first row of table if where() condition is not found otherwise it gives the first matched row of given criteria.
This works for me: (PHP: 7.0 - Laravel 5.5)
use DB; $statement = DB::select("SHOW TABLE STATUS LIKE 'users'"); $nextId = $statement[0]->Auto_increment;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With