Laravel Packages

Laravel Date Filtering Package for Eloquent

Published
Laravel Date Filtering Package for Eloquent image

This Laravel Date Filtering package aims to simplify date-based filtering for your Laravel Eloquent models. It provides various methods to filter records based on time and date intervals:

use OmarElnaghy\LaraDateFilters\Enums\DateRange;
 
$startDate = Carbon::parse('2023-9-03');
 
// Generic method to filter by various units
Post::filterByDateRange(
duration: 2,
dateUnit: 'day',
date: $startDate,
direction: 'before', // or `'after'`
range: DateRange::INCLUSIVE,
)->get();
 
// Helpers for each unit type: second, minute, hour, etc.
Post::filterByDateMinutesRange(
duration: 2,
date: $startDate,
direction: 'after',
range: DateRange::EXCLUSIVE,
)->get();
 
/*
Post::filterByDateSecondsRange();
Post::filterByDateHoursRange();
Post::filterByDateDaysRange();
Post::filterByDateWeeksRange();
Post::filterByDateMonthsRange();
*/

The setup for this package is defining a custom Eloquent builder that extends the built-in builder with this added functionality. It gives you access to the above filter methods as well as dynamic ranges on the fly using the following conventions:

Post::filterByDate5DayRange($startDate, $direction, $range)->get();
Post::filterByDate6WeekRange($startDate, $direction, $range)->get();
Post::filterByDate7MonthRange($startDate, $direction, $range)->get();

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article
Queue totalSize() and JobInterrupted Event in Laravel 13.31 image

Queue totalSize() and JobInterrupted Event in Laravel 13.31

Read article
Find Unexpected Test Inputs with Fuzz for Pest image

Find Unexpected Test Inputs with Fuzz for Pest

Read article