You cannot rely on a solution of your own to get 100% unique IDs considering the length you are looking at. I suggest that you should use an auto-incremented primary key and after creating the record, update your custom ID column with a value created using the primary key.
For example; create a record and get its integer ID. And then update the custom ID column something like this:
$user = User::create($data);
$user->custom_id = sprintf('user-%s', $user->id);
$user->save();
This way, you will be able to use a short ID and will also ensure that the ID is unique.
But as suggested in other answers, you should be using a UUID library. That's the solution that has been developed after years of use cases.
p.s. With your own solution that you specified, you will experience 1000s of collisions before reaching the 10 million target. Moreover, I used User::create()
as an example to create a record just because you have tagged this with laravel
.