Guest User

Untitled

a guest
Jun 5th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models;
  4.  
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8.  
  9. use App\Models\User;
  10.  
  11. class Advisers extends Model
  12. {
  13. use HasFactory;
  14. use SoftDeletes;
  15.  
  16. protected $guarded = [];
  17.  
  18. protected $fillable = [
  19. 'image',
  20. 'firstname',
  21. 'middlename',
  22. 'lastname',
  23. 'extensionname',
  24. 'name',
  25. 'email',
  26. 'note',
  27. 'password',
  28. 'deleted_at'
  29. ];
  30.  
  31. // AUTO FILL 'FULLNAME'
  32. protected static function booted()
  33. {
  34. static::saving(function ($adviser) {
  35. $adviser->name = trim("{$adviser->firstname} {$adviser->middlename} {$adviser->lastname} {$adviser->extensionname}");
  36. });
  37. }
  38.  
  39.  
  40. public function user()
  41. {
  42. return $this->hasOne(User::class, 'id', 'id');
  43. }
  44.  
  45.  
  46. protected static function boot()
  47. {
  48. parent::boot();
  49.  
  50. static::updated(function ($adviser) {
  51. if ($adviser->user) {
  52. $adviser->user->update([
  53. 'name' => $adviser->name,
  54. 'email' => $adviser->email,
  55. 'password' => $adviser->password,
  56. 'deleted_at' => $adviser->deleted_at,
  57. ]);
  58. \Log::info("UPDATED");
  59. }
  60. });
  61.  
  62. static::deleted(function ($adviser) {
  63. if ($adviser->user) {
  64. $adviser->user->delete();
  65. \Log::info("DELETED");
  66. }
  67. });
  68.  
  69. static::restored(function ($adviser) {
  70. \Log::info("RESTORED event triggered for Adviser ID: " . $adviser->id);
  71.  
  72. if ($adviser->user) {
  73. $adviser->user->restore();
  74. \Log::info("User restored successfully");
  75. }
  76. });
  77.  
  78. }
  79.  
  80.  
  81.  
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment