It seems like you're encountering an issue where the auto-increment sequence on your table is not set correctly. When you use insertGetId, it should automatically use the next available auto-increment value for the id column. If it's starting from 1, it could be because the sequence is out of sync with the actual data in the table.
Here's what you can do to resolve this issue:
- First, check the current maximum
idin your table to understand what the nextidshould be:
SELECT MAX(id) FROM your_table_name;
- Then, you need to reset the auto-increment value to be one greater than the maximum
idyou just found. You can do this with the following SQL command (replaceyour_table_namewith the actual table name andnext_id_valuewith the nextidvalue you want to start from):
ALTER TABLE your_table_name AUTO_INCREMENT = next_id_value;
- If you're using Laravel's Eloquent or Query Builder, you can execute a raw statement to set the auto-increment value like this:
DB::statement("ALTER TABLE your_table_name AUTO_INCREMENT = next_id_value;");
Make sure to replace your_table_name with the actual table name and next_id_value with the correct value based on the maximum id you found.
Here's how you might integrate this into your existing code:
$maxId = DB::connection($this->database->name)
->table($this->schema_name.'.'.$destination_table_name)
->max('id');
$nextId = $maxId + 1;
DB::connection($this->database->name)
->statement("ALTER TABLE {$this->schema_name}.{$destination_table_name} AUTO_INCREMENT = {$nextId};");
$insertedId = DB::connection($this->database->name)
->table($this->schema_name.'.'.$destination_table_name)
->insertGetId($inserted_data);
// Continue with your logic
Please note that directly manipulating the AUTO_INCREMENT value of a table should be done with caution, and it's important to ensure that the value you're setting it to does not conflict with existing ids in the table. Also, make sure you have the necessary permissions to alter the table structure.
If you're still facing issues, it might be helpful to check for any triggers or database events that could be affecting the id column or the auto-increment behavior.