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

somenet77's avatar

Why lazy load return empty collection

I have a package that has multiple departures.

$package->load(['departures' => function ($query) {
    $query->select(DB::raw("distinct(DATE_FORMAT(start_date,'%Y-%m')) as start_date"))
        ->whereDate('start_date', '>', today())
        ->orderBy('start_date', 'asc');
}]);

but this return an empty unique date collection.

PackageDeparture::where('package_id', $package->id)
    ->whereDate('start_date', '>', date('Y-m-d'))
    ->select(DB::raw("distinct(DATE_FORMAT(start_date,'%Y-%m')) as start_date"))
    ->orderBy('start_date', 'asc')
    ->get();

while this return all unique dates.

0 likes
1 reply

Please or to participate in this conversation.