The Filament table ran a query per row
Dot notation tells Filament which relationships a column needs. Anything else — an accessor, a computed state, a count in a closure — runs inside the row loop, and preventLazyLoading only catches half of it.
ON THIS PAGE
Two harmless-looking columns
The posts table in your admin panel used to be instant. Then somebody asked for two small things: show the author next to each title, and show how many comments each post has. Both landed in one pull request, both looked trivial, and the list page now takes noticeably longer every time someone changes the page size to 50.
Here is the table after that pull request:
TextColumn::make('title'),
TextColumn::make('byline'),
TextColumn::make('comments')
->state(fn (Post $record): int => $record->comments()->count()),
And byline is an accessor on the model:
protected function byline(): Attribute
{
return Attribute::get(fn () => $this->title.' by '.$this->author->name);
}
Neither column mentions a query. Both run one for every row on the page.
What Filament can see, and what it cannot
Filament does eager-load relationships for you, but only when the column name tells it about one. The Filament docs describe it directly: for a dot-notation column like author.name, Filament uses the dot notation to eager-load the results of that relationship. Under the hood, the column inspects its own name, finds author is a relation on the model, and adds ->with(['author']) to the table query before it runs.
A column named byline contains no dot. Filament reads the attribute, Eloquent calls the accessor, and the accessor touches $this->author on a model that never had it loaded. That is a lazy load, one per row. The state() closure is worse in a different way: $record->comments()->count() is not a relationship access at all, it is an explicit query, and it runs for every record by design.
Measured on the list page of a scratch application, ten authors with five posts each, default page size of 10:
relation-only => 3 queries {"posts":2,"authors":1}
naive => 22 queries {"posts":2,"authors":10,"comments":10}
The relation-only table used author.name — two queries for the paginated posts (a count and the page), one for all their authors. The naive table did those same two, then went back to the database twice for each row. The query log shows the pattern you are looking for:
select * from "posts" order by "posts"."id" asc limit 10 offset 0
select * from "authors" where "authors"."id" = ? limit 1
select count(*) as "aggregate" from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null
select * from "authors" where "authors"."id" = ? limit 1
select count(*) as "aggregate" from "comments" where "comments"."post_id" = ? and "comments"."post_id" is not null
Two extra queries per row is 22 at ten rows and 102 at fifty. Each one is fast. The total is not, especially on a hosted database where every round trip costs a few milliseconds of network before the query even starts.
The fix: tell the query what the row needs
For the accessor, load the relationship it depends on. On a resource's list page, modifyQueryUsing() on the table is the place for it:
return $table
->modifyQueryUsing(fn (Builder $query) => $query->with('author'))
->columns([
TextColumn::make('title'),
TextColumn::make('byline'),
TextColumn::make('comments_count')->counts('comments'),
]);
For the count, stop computing it in PHP. Filament's counts() method adds a withCount() to the table query, so the number arrives as a subquery on the same select. The column must be named the way Eloquent names the count attribute: comments_count for a comments relationship.
Same data, same page:
fixed => 3 queries
The accessor still runs on every row; it just finds author already loaded. If you can express the column as author.name instead of an accessor, do — Filament will add the eager load for you and the next person to touch the table cannot forget it. The two approaches also mix safely: before adding its own with(), the column checks whether the relationship is already in the query's eager loads, so a manual with('author') alongside an author.name column does not load the authors twice.
preventLazyLoading catches one of the two
Laravel's guard for this is Model::preventLazyLoading(), usually switched on outside production in AppServiceProvider:
public function boot(): void
{
Model::preventLazyLoading(! $this->app->isProduction());
}
With it on, the naive table refuses to render at all:
Attempted to lazy load [author] on model [App\Models\Post] but lazy loading is disabled.
That is the accessor, caught. Now remove the accessor and keep only the comment count closure. The page renders without complaint, and the query log still has one count per row:
closure-only with preventLazyLoading: no exception, 12 queries
preventLazyLoading() fires when you access a relationship property that was not loaded. $record->comments()->count() calls the relationship method and asks for a fresh query on purpose, so there is nothing for the guard to object to. The same applies to $record->author()->first(), to Author::find($record->author_id) inside a formatStateUsing() closure, and to any service you call per row that happens to query. Treat every closure that receives $record as a place a query can hide.
Catching the ones the guard misses
Since the guard cannot see explicit queries, count them. A feature test that renders the list page and asserts an upper bound on queries does the job without caring where they come from:
it('renders the posts table in a fixed number of queries', function () {
Post::factory()->count(25)->create();
$this->actingAs(User::factory()->create(['is_admin' => true]));
DB::enableQueryLog();
Livewire::test(ListPosts::class)->assertOk();
expect(DB::getQueryLog())->toHaveCount(3);
});
Run against the scratch application with 25 posts in the table, the first render of the fixed table took three queries and the naive one took 22:
fixed first render: 3 queries
naive first render: 22 queries
Note that 25 posts still produced 22 queries, not 52. The table renders one page, ten rows by default, so the per-row cost is multiplied by the page size rather than the table size. That is also why this bug hides in development, where the seeder makes a dozen rows and nobody changes the page size, and appears in production the first week somebody sets it to 50.
Pin the number at whatever your page needs today. Adding rows must not change it; adding a careless column will, and the test will tell you which pull request did it.
What to change
Open each Filament table and read its columns looking for three shapes: a column name with no dot that is backed by an accessor touching a relationship; a state() or formatStateUsing() closure that calls a relationship method or a model query; and a count, sum or existence check computed in PHP. The first needs a with() in modifyQueryUsing(), or better, a dot-notation column. The second and third should become counts(), or the matching aggregate column, or a value selected in the table query.
Then turn on preventLazyLoading() for every environment that is not production, and add one query-count test per busy table. The guard catches the accessors; the test catches everything else.