@kikismedia does it need to be a number? why not generate a hash?
Jan 22, 2021
9
Level 5
how can I generate 10 digit unique number that will be sent to user after registration on laravel
I would like to generate a 10 unique number for a user immediately they Register on the website sent via email and probably they will use the generated number to login to the website , teach me how to achieve this or help with directives I'm new to laravel and php
Level 2
Here, this is how I generate random 5 digit unique number for each user:
//Generating a random unique candidate_id for each applicant & adding 0 in upfront if id less than 5 digits
$candidatesData = Candidate::get(['candidate_id'])->toArray();
do {
$applicant_id = str_pad(rand(1, 5000), 5, "0", STR_PAD_LEFT);
} while (in_array($applicant_id, $candidatesData));
$candidate = Candidate::create($request->all());
$candidate->candidate_id = $applicant_id;
$candidate->save();
Please consider refactoring the code as per your needs.
2 likes
Please or to participate in this conversation.