Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mmoollllee's avatar

Custom Column with ManyToMany Relationship

Hi everyone! 😊

I'm working with Filament PHP and have a many-to-many relationship between Machines and Tests:

Machine belongsToMany Tests

Test belongsToMany Machines

The goal is to track regular inspections of machines. Each Test has a next due date (next_time). In the Machines table, I want to display the next due date (defined from the latest test) for each machine. Ideally, I should also be able to sort by this column.

What I have so far:

In my Machine model, I retrieve the last test like this:

public function last_test(): ?Test
{
    return $this->tests()
        ->orderBy('next_time', 'desc')
        ->first();
}

I also created a custom Filament Table Column to display the next due date and link to the latest test:

Issues I'm facing

  • Sorting doesn't work on this column.
  • A new query is executed for each row, which isn't optimal.
  • The data appears in the next row instead of the correct one and I don't understand why).

Can someone guide me on the correct approach to achieve this? 🤔

Thanks in advance! 😊

0 likes
1 reply

Please or to participate in this conversation.