Laravel Tutorials

Streamlining Date Queries with Laravel's Shorthand Methods

Published
Streamlining Date Queries with Laravel's Shorthand Methods image

Laravel enhances date-based querying with expressive shorthand methods that simplify common temporal filters in your database queries. These intuitive methods reduce boilerplate while making time-based conditions more readable.

The basic implementation demonstrates the clear, semantic syntax:

// Find records with dates in the past
$expiredSubscriptions = Subscription::wherePast('expires_at')->get();

// Find records with dates in the future
$upcomingEvents = Event::whereFuture('starts_at')->get();

// Find records with today's date
$todaysAppointments = Appointment::whereToday('scheduled_for')->get();

Here's how these methods streamline queries in a booking management system:

class ReservationController extends Controller
{
    public function dashboard()
    {
        return view('reservations.dashboard', [
            'upcoming' => Reservation::whereFuture('check_in')
                ->where('status', 'confirmed')
                ->orderBy('check_in')
                ->get(),
                
            'active' => Reservation::whereToday('check_in')
                ->orWhere(function($query) {
                    $query->whereToday('check_out')
                        ->whereNotNull('checked_in_at');
                })
                ->get(),
                
            'past' => Reservation::wherePast('check_out')
                ->latest('check_out')
                ->limit(10)
                ->get(),
                
            'pending_reviews' => Reservation::wherePast('check_out')
                ->whereNull('reviewed_at')
                ->where('status', 'completed')
                ->get()
        ]);
    }
}

These date shorthand methods create self-documenting queries that clearly express your temporal constraints without the complexity of manual date comparisons.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux image

pgjumbo: A PostgreSQL Client for macOS, Windows, and Linux

Read article
Laranon: Reversible PII Anonymization for Laravel and LLMs image

Laranon: Reversible PII Anonymization for Laravel and LLMs

Read article
PHP 8.6 is released!  image

PHP 8.6 is released!

Read article
Laravel Warrant: Row-Level Permissions With Eloquent image

Laravel Warrant: Row-Level Permissions With Eloquent

Read article
JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43 image

JSON Schema Validation Rules and a RetryFor Attribute in Laravel 13.43

Read article
Durable Workflow: Long-Running Workflows for Laravel image

Durable Workflow: Long-Running Workflows for Laravel

Read article