-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.php
36 lines (25 loc) · 1.07 KB
/
generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
function generateUniqueId($table, $column) {
// Check if the required arguments are provided
if (empty($table) || empty($column)) {
throw new InvalidArgumentException('Missing required arguments');
}
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$id = '';
$unique = false;
while (!$unique) {
$id = '';
for ($i = 0; $i < 25; $i++) {
$id .= $characters[rand(0, strlen($characters) - 1)];
}
// Check if the ID already exists in the database (laravel)
$count = DB::table($table)->where($column, $id)->count();
// Check if the ID already exists in the database (native php)
// $count = SELECT COUNT(*) FROM table WHERE column = id;
if ($count == 0) {
$unique = true;
}
}
return $id;
}
?>