php - How to generate unique Voucher code in laravel 5.2? -
i want save unique voucher code , mix of characters , numerics , should 6 in length. using laravel framework 5.2
enter code here $data = $request->all(); unset($data['_token']); //echo "<pre>"; print_r($data); die; ($i=1; $i <=$data['countvoucher']; $i++) { $voucher = new voucher; $voucher->code = "123456";// should dynamic , unique $voucher->percentage = $data['percentage']; $voucher->usage = $data['usage']; $voucher->expirydate = $data['expirydate']; $voucher->save(); }
$voucher->code want save in filed can me
i using function may use bellow
$voucher->code = $this->generaterandomstring(6);// should dynamic , unique public function generaterandomstring($length = 20) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'; $characterslength = strlen($characters); $randomstring = ''; ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, $characterslength - 1)]; } return $randomstring; }
Comments
Post a Comment