0

I am trying to store unique and random student_registration_id number. If I create 5000 user registration that time also it should be unique and it should 10 digit and also I am storing student image below image storing unique id is perfect or not.

Code

public function store(Request $request)
{

  $this->validate($request, [

      'student_name' => 'required|string|max:255',
      'student_father_name' => 'required|string|max:255',
      'student_mother_name' => 'required|string|max:255',
      'student_photo' => 'required|image|mimes:jpeg,png,jpg|max:2048',  

]);

    $input['student_photo'] = time().'.'.$request->student_photo->getClientOriginalExtension();
    $folder1 = public_path('STUDENT_DATA/STUDENT_PHOTO/');
    $path1 = $folder1 . $input['student_photo']; // path 1
    $request->student_photo->move($folder1, $input['student_photo']); // image saved in first folder
    $path2 = public_path('../../../abc.com/public/STUDENT_DATA/STUDENT_PHOTO/') . $input['student_photo']; // path 2
    \File::copy($path1, $path2);

       $input['student_name'] = strtoupper ($request['student_name']);
       $input['student_father_name'] = strtoupper ($request['student_father_name']);
       $input['student_mother_name'] = strtoupper ($request['student_mother_name']);

       $input['student_registration_id'] ="SIIT_".time();        
    Student::create($input);    
   return back()->with('success',' STUDENT REGISTERD SUCCESSFULLY .');
}
5
  • 1
    use time function with unique id & md5 function like this. md5(uniqid().time()) Commented Dec 13, 2019 at 10:23
  • @Webbinion who to maintain 10 digit Commented Dec 13, 2019 at 10:28
  • 1
    Does this answer your question? How to generate unique random value for each user in laravel and add it to database Commented Dec 13, 2019 at 10:38
  • Answer accepted in @pringi's link is a correct answer to your problem and to be absolutely certain to never have collision. Commented Dec 13, 2019 at 13:12
  • @cbaconnier how to implement pringi provided linked answer in my code i am confused Commented Dec 13, 2019 at 13:45

2 Answers 2

4

Per your request, here's how to implement the suggested answer with your code


public function store(Request $request)
{

  $this->validate($request, [
      'student_name' => 'required|string|max:255',
      'student_father_name' => 'required|string|max:255',
      'student_mother_name' => 'required|string|max:255',
      'student_photo' => 'required|image|mimes:jpeg,png,jpg|max:2048',  
]);

    $input['student_photo'] = time().'.'.$request->student_photo->getClientOriginalExtension();
    $folder1 = public_path('STUDENT_DATA/STUDENT_PHOTO/');
    $path1 = $folder1 . $input['student_photo']; // path 1
    $request->student_photo->move($folder1, $input['student_photo']); // image saved in first folder
    $path2 = public_path('../../../abc.com/public/STUDENT_DATA/STUDENT_PHOTO/') . $input['student_photo']; // path 2
    \File::copy($path1, $path2);

    $input['student_name'] = strtoupper ($request['student_name']);
    $input['student_father_name'] = strtoupper ($request['student_father_name']);
    $input['student_mother_name'] = strtoupper ($request['student_mother_name']);

    $id = $this->generateRegistrationId();
    $input['student_registration_id'] = $id;
    DB::table('locations')->insert([['center_code' => $id]])

    Student::create($input); 

   return back()->with('success',' STUDENT REGISTERD SUCCESSFULLY .');
}

function generateRegistrationId() {
    $id = 'SIIT_' . mt_rand(1000000000, 9999999999); // better than rand()

    // call the same function if the id exists already
    if ($this->registrationIdExists($id)) {
        return $this->generateRegistrationId();
    }

    // otherwise, it's valid and can be used
    return $id;
}

function registrationIdExists($id) {
    // query the database and return a boolean
    // for instance, it might look like this in Laravel
    return Student::where('student_registration_id', $id)->exists();
}
Sign up to request clarification or add additional context in comments.

3 Comments

how to maintain digits length
What do you mean? 'SIIT_' . mt_rand(1000000000, 9999999999); will generate a number between 1000000000 to 9999999999 which will always be 10 digits as you desired. Example of id generated : 'SIIT_1234567890
i what to save id value hear also but value are not same DB::table('locations')->insert([['center_code' => $this->generateRegistrationId()]]);
0

You could use this: hexdec(uniqid()); uniqid() - returns unique numbers but in their hexadecimal representation and therefore you will have to use: hexdec() to convert it to their decimal representation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.