Hi,
I am trying to get the next autoincrement id of a table in laravel. i.g. current value of auto-increment id is 4. So the next will 5, right?
Can I somehow get the next autoincrment id?
@eugenevdm I am mostly curious how getting the next ID is useful at all. Chances are that the ID is taken by another user, by the time the page is stored
The reason it is problematic is this. Lets say it returns 45. But what you don't know is that there was a 46 earlier that was deleted. So you tell the system that the next id will be 46, when in reality it will be 47
if you have more than one user what you think is the next number can be taken immediately after you read it and before you use it. Two users end up with the same number
if you use a transaction, and it fails and gets rolled back, there will be a gap in the numbers
@Snapey
I actually have a case for this, I need to import data from another system with different ID, I need to know the new ID so I can have a mapping between the two system, and use the new ID to import all related data.
Hi everyone)
I have an ideea. You can use uuid, but for this, you need to change primary id in your table.
In your migration you need to change $table->id(); to $table->uuid('id')->primary();
In your model you need to add use HasUuids; (use Illuminate\Database\Eloquent\Concerns\HasUuids;)