belongsTo(Account::class); } public function getNameAttribute() { return $this->first_name.' '.$this->last_name; } public function setPasswordAttribute($password) { $this->attributes['password'] = Hash::make($password); } public function scopeOrderByName($query) { $query->orderBy('last_name')->orderBy('first_name'); } public function scopeWhereRole($query, $role) { switch ($role) { case 'user': return $query->where('owner', false); case 'owner': return $query->where('owner', true); } } public function scopeFilter($query, array $filters) { $query->when($filters['search'] ?? null, function ($query, $search) { $query->where(function ($query) use ($search) { $query->where('first_name', 'ilike', '%'.$search.'%') ->orWhere('last_name', 'ilike', '%'.$search.'%') ->orWhere('email', 'ilike', '%'.$search.'%'); }); })->when($filters['role'] ?? null, function ($query, $role) { $query->whereRole($role); })->when($filters['trashed'] ?? null, function ($query, $trashed) { if ($trashed === 'with') { $query->withTrashed(); } elseif ($trashed === 'only') { $query->onlyTrashed(); } }); } }