belongsTo(Organization::class); } public function getNameAttribute() { return $this->first_name.' '.$this->last_name; } public function scopeOrderByName($query) { $query->orderBy('last_name')->orderBy('first_name'); } public function scopeFilter($query, array $filters) { $query->when($filters['search'] ?? null, function ($query, $search) { $query->where(function ($query) use ($search) { $query->where('first_name', 'like', '%'.$search.'%') ->orWhere('last_name', 'like', '%'.$search.'%') ->orWhere('email', 'like', '%'.$search.'%') ->orWhereHas('organization', function ($query) use ($search) { $query->where('name', 'like', '%'.$search.'%'); }); }); })->when($filters['trashed'] ?? null, function ($query, $trashed) { if ($trashed === 'with') { $query->withTrashed(); } elseif ($trashed === 'only') { $query->onlyTrashed(); } }); } }