How to generate unique random value for each user in laravel and add it to database
Your logic isn't technically faulty. However, if your application attracts lots of users, fetching all of the random numbers may well become unnecessarily expensive, in terms of resources and computation time.
I would suggest another approach, where you generate a random number and then check it against the database.
function generateBarcodeNumber() { $number = mt_rand(1000000000, 9999999999); // better than rand() // call the same function if the barcode exists already if (barcodeNumberExists($number)) { return generateBarcodeNumber(); } // otherwise, it's valid and can be used return $number;}function barcodeNumberExists($number) { // query the database and return a boolean // for instance, it might look like this in Laravel return User::whereBarcodeNumber($number)->exists();}