Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use App\Models\User;
- class Advisers extends Model
- {
- use HasFactory;
- use SoftDeletes;
- protected $guarded = [];
- protected $fillable = [
- 'image',
- 'firstname',
- 'middlename',
- 'lastname',
- 'extensionname',
- 'name',
- 'email',
- 'note',
- 'password',
- 'deleted_at'
- ];
- // AUTO FILL 'FULLNAME'
- protected static function booted()
- {
- static::saving(function ($adviser) {
- $adviser->name = trim("{$adviser->firstname} {$adviser->middlename} {$adviser->lastname} {$adviser->extensionname}");
- });
- }
- public function user()
- {
- return $this->hasOne(User::class, 'id', 'id');
- }
- protected static function boot()
- {
- parent::boot();
- static::updated(function ($adviser) {
- if ($adviser->user) {
- $adviser->user->update([
- 'name' => $adviser->name,
- 'email' => $adviser->email,
- 'password' => $adviser->password,
- 'deleted_at' => $adviser->deleted_at,
- ]);
- \Log::info("UPDATED");
- }
- });
- static::deleted(function ($adviser) {
- if ($adviser->user) {
- $adviser->user->delete();
- \Log::info("DELETED");
- }
- });
- static::restored(function ($adviser) {
- \Log::info("RESTORED event triggered for Adviser ID: " . $adviser->id);
- if ($adviser->user) {
- $adviser->user->restore();
- \Log::info("User restored successfully");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment