Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set AUTO_INCREMENT in Laravel with Eloquent?

I am working on an application and my employer wants that the id in the user table should begin from 100,000 instead of 1. How can accomplish that? Is it a parameter in the Schema Builder itself? Or would I have to set something in the MySQL instead?

like image 566
Rohan Avatar asked Oct 16 '25 14:10

Rohan


2 Answers

You can use SQL its self to do this. ALTER TABLE <table_name> AUTO_INCREMENT=100000;.

Or

Do it like here

And use a unprepared statement;

$statement = "ALTER TABLE MY_TABLE AUTO_INCREMENT = 100000;";

DB::unprepared($statement);

or

DB::update("ALTER TABLE {your table name} AUTO_INCREMENT = 100000;");

Which could then be put within the migration that creates the table.

like image 126
Matt Burrow Avatar answered Oct 19 '25 03:10

Matt Burrow


In Laravel 8 you can use from() or startingValue() in schema while creating the table to set the autoincrement

$table->id()->from(100000);

or use

$table->id()->startingValue(100000);

like image 31
Srinivasa K C Avatar answered Oct 19 '25 03:10

Srinivasa K C