Vahid Talebi
Vahid Talebi

Reputation: 73

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

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;

Upvotes: 0

Views: 1075

Answers (1)

Prajan Acharya
Prajan Acharya

Reputation: 51

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.

Code Snippet to Support the Answer

Upvotes: 1

Related Questions