FilamentF
Filament14mo ago
403gtfo

Is sortable() and/or searchable() possible on a HasOneThrough relation?

I don't think I have asked this before...
But it is a hard one.

The setup:
  • One Staff can have many positions.
  • One Position has one Discipline.
  • Positions have an end_date field
The question:
Is it possible to have a HasOneThrough relation work with sortable() on the StaffResource?


Relation:
 public function latestDiscipline(): HasOneThrough
  { {
      return $this->hasOneThrough(Discipline::class, Position::class, 'staff_id', 'id', 'id', 'discipline_id')
        ->where('is_active', true)
        ->orderBy('end_date', 'desc');
    }
  }

  public function getLatestDisciplineNameAttribute()
  {
    $latestDiscipline = $this->latestDiscipline()->first();
    return $latestDiscipline ? $latestDiscipline->name : null;
  }


Text column:
TextColumn::make('latest_discipline_name')
    ->label('Discipline')
    // ->searchable()
    ->sortable()
    ->toggleable(),
Was this page helpful?