1 | <?php |
||
2 | |||
3 | namespace App\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use App\Models\Base_Model; |
||
7 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
8 | use Webpatser\Uuid\Uuid; |
||
9 | |||
10 | class Student_guardian extends Base_Model { |
||
11 | |||
12 | use SoftDeletes; |
||
13 | |||
14 | public const CREATED_AT = 'created'; |
||
15 | |||
16 | public const UPDATED_AT = 'modified'; |
||
17 | |||
18 | |||
19 | /** |
||
20 | * The database table used by the model. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $table = 'student_guardians'; |
||
25 | |||
26 | protected $softDelete = true; |
||
27 | |||
28 | /** |
||
29 | * Attributes that should be mass-assignable. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $fillable = ['student_id', 'guardian_id', 'guardian_relation_id', 'modified_user_id', 'modified', 'created_user_id', 'created']; |
||
34 | |||
35 | /** |
||
36 | * The attributes excluded from the model's JSON form. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $hidden = []; |
||
41 | |||
42 | /** |
||
43 | * The attributes that should be casted to native types. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $casts = []; |
||
48 | |||
49 | /** |
||
50 | * The attributes that should be mutated to dates. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $dates = ['modified', 'created']; |
||
55 | |||
56 | |||
57 | public $timestamps = false; |
||
58 | |||
59 | public static function boot() |
||
60 | { |
||
61 | parent::boot(); |
||
62 | self::creating(function ($model) { |
||
63 | $model->id = (string) Uuid::generate(4); |
||
64 | $model->created_user_id = 1; |
||
65 | }); |
||
66 | } |
||
67 | |||
68 | public static function createStudentGuardian($student,$guardian,$user){ |
||
69 | |||
70 | $exist = true; |
||
71 | if(!is_null($guardian)) |
||
72 | $exist = self::where('student_id', $student->student_id) |
||
73 | ->where('guardian_relation_id', $guardian->guardian_relation_id) |
||
74 | ->exists(); |
||
75 | |||
76 | $totalGuardians = self::where('student_id',$student->student_id)->count(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
77 | |||
78 | $data = [ |
||
79 | 'student_id' => $student->student_id, |
||
80 | 'guardian_id' => $guardian->id, |
||
81 | 'guardian_relation_id' => $guardian->guardian_relation_id, |
||
82 | 'created_user_id' => $user |
||
83 | ]; |
||
84 | if(!$exist){ |
||
85 | $data['created'] = now(); |
||
86 | self::create($data); |
||
87 | }else{ |
||
88 | $data['modified'] = now(); |
||
89 | self::where('student_id' , $student->student_id) |
||
90 | ->where('guardian_relation_id',$guardian->guardian_relation_id) |
||
91 | ->update($data); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | } |