19 lines
492 B
PHP
19 lines
492 B
PHP
<?php
|
|
|
|
if (!function_exists('generate_unique_code')) {
|
|
function generate_unique_code($model, $column = 'code', $length = 6): string
|
|
{
|
|
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
$max = strlen($characters) - 1;
|
|
|
|
do {
|
|
$code = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$code .= $characters[random_int(0, $max)];
|
|
}
|
|
} while ($model::where($column, $code)->exists());
|
|
|
|
return $code;
|
|
}
|
|
}
|