I want to get the next id that will be created but not yet in laravel

All we need is an easy explanation of the problem, so here it is.

I want to get the next id that will be created but not yet in laravel.
Is this code correct?

$tableStatus = DB::select("show table status from database_name where Name = 'table_name'");
if (empty($tableStatus)) {
    throw new \Exception("Table not found");
}
// Get first table result, get its next auto incrementing value
echo $tableStatus[0]->Auto_increment;

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Yes, the code is correct.
You can achive this in several other ways. For example, You can query the particular database table to extract last created id by ordering the rows based on created_at column or id column itself and finally increment the value by 1 to get next auto-increment value.

I want to get the next id that will be created but not yet in laravel

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply