为了账号安全,请及时绑定邮箱和手机立即绑定

在 laravel 中如何创建唯一和随机数

在 laravel 中如何创建唯一和随机数

PHP
慕斯709654 2022-06-17 10:16:33
我正在尝试存储唯一且随机的 student_registration_id 编号。如果我当时创建了 5000 个用户注册,它也应该是唯一的,它应该是 10 位数字,而且我在存储唯一 ID 的图像下方存储学生图像是否完美。代码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 .');}
查看完整描述

2 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

根据您的要求,以下是如何使用您的代码实施建议的答案



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();

}


查看完整回答
反对 回复 2022-06-17
?
达令说

TA贡献1821条经验 获得超6个赞

您可以使用: hexdec(uniqid()); uniqid()- 返回唯一数字,但以十六进制表示,因此您必须使用: hexdec()将其转换为十进制表示。



查看完整回答
反对 回复 2022-06-17
  • 2 回答
  • 0 关注
  • 175 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信