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

Sinres's avatar

Get records where created_at the date is less today

Hello!

How I can get records where created_at the date is less today but not older less 5 days?

Control::with(['shop.documents' => function ($query) {
            $query->whereIn('doc_type', [1,4]);
        }])->whereDate('created_at', '<=', today()->subDays(5))
            ->whereDate('created_at', '<=', today())
            ->get()

I trying some like this but this is not working.

Thanks

0 likes
2 replies
tykus's avatar
Control::with(['shop.documents' => function ($query) {
    $query->whereIn('doc_type', [1,4]);
}])->whereBetween('created_at', [today()->subDays(5), today()])
    ->get()

Your original effort has the incorrect comparison ->whereDate('created_at', '<=', today()->subDays(5)); it should be

->whereDate('created_at', '>=', today()->subDays(5))
Snapey's avatar

and try not to use magic numbers

$query->whereIn('doc_type', [1,4]);

use constants or enums

How does someone reading your code know what 1 or 4 means?

Please or to participate in this conversation.