亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 213 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號