1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Lsf\UniqueUid\UniqueUid; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use PhpParser\Node\Expr\Throw_; |
9
|
|
|
|
10
|
|
|
class Unique_user_id extends Model |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The database table used by the model. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $table = 'unique_user_id'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Attributes that should be mass-assignable. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $fillable = ['unique_id', 'security_user_id']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The attributes excluded from the model's JSON form. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $hidden = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The attributes that should be casted to native types. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $casts = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The attributes that should be mutated to dates. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $dates = []; |
47
|
|
|
|
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
$this->uniqueUserId = new UniqueUid(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function updateOrInsertRecord($user) |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
|
|
// regenerate unique id if it's not available |
59
|
|
|
$uniqueId = $this->uniqueUserId::isValidUniqueId($user['openemis_no'],9) ? $this->uniqueUserId::getUniqueAlphanumeric() : $user['openemis_no']; |
60
|
|
|
|
61
|
|
|
//check if the user's entry exits ? |
62
|
|
|
$exists = Unique_user_id::where('unique_id' , $uniqueId)->exists(); |
63
|
|
|
|
64
|
|
|
if (!$exists) { |
65
|
|
|
// try to feed unique user id |
66
|
|
|
Unique_user_id::insert([ |
67
|
|
|
'security_user_id' => $user['id'], |
68
|
|
|
'unique_id' => $uniqueId |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
return $user; |
72
|
|
|
} catch (\Exception $th) { |
73
|
|
|
Log::error($th->getMessage()); |
74
|
|
|
$user['openemis_no'] = $this->uniqueUserId::getUniqueAlphanumeric(); |
75
|
|
|
$this->updateOrInsertRecord($user); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.