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

Frondor's avatar

Eager loading executes same query twice.

I wonder why this code generates two queries, is it working as intended or something's wrong with it?

\DB::enableQueryLog();
        $user = $request->user()->load(['reservations' => function ($query) use ($now, $time) {
            $query->where('date', '>', $now);
            $query->where('time', '>', $time);
            $query->where('cancelled', false);
            $query->orderBy('time');
            $query->first();
        }]);
dd( \DB::getQueryLog() );

0 likes
1 reply
Frondor's avatar
Frondor
OP
Best Answer
Level 1

Got it.. $query->first() is the evil one here.

1 like

Please or to participate in this conversation.