Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get next id of autogenerated field in laravel for specific table?

Tags:

I am looking for something like :

DB::table('users')->getNextGeneratedId(); 

not

$user->save($data) $getNextGeneratedId = $user->id; 

Does anybody know how to achieve this?

like image 398
fico7489 Avatar asked May 13 '16 13:05

fico7489


People also ask

How can I get next insert ID in laravel?

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.

What does get () do in laravel?

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() .

How can I get only one record in laravel?

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.


1 Answers

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; 
like image 184
Leandro Parice Avatar answered Oct 09 '22 09:10

Leandro Parice