Streamlining Date Queries with Laravel's Shorthand Methods

Last updated on by

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum

The latest

View all →
Yammi Audit Log: Track Who Really Made a Change Across Jobs and Queues image

Yammi Audit Log: Track Who Really Made a Change Across Jobs and Queues

Read article
Route Metadata Support in Laravel 13.17 image

Route Metadata Support in Laravel 13.17

Read article
Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents image

Ship AI with Laravel: Failover, Queues, and Middleware for AI Agents

Read article
Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower image

Monitor and Control Schedules, Queues, and Errors in Laravel with Watchtower

Read article
Showcase Your PhpStorm Expertise on LinkedIn image

Showcase Your PhpStorm Expertise on LinkedIn

Read article
Privacy Filter: Detect PII in Text from Laravel image

Privacy Filter: Detect PII in Text from Laravel

Read article