Blog Detail

26

Sep
A Laravel Package for Advanced Date Filtering & Manipulation cover image

arrow_back A Laravel Package for Advanced Date Filtering & Manipulation

Laravel Date Filtering is a package that simplifies date-based filtering for your Laravel Eloquent models. It provides a set of convenient methods to filter records based on various date and time intervals.

Installation

You can install the package via Composer:

composer require omar-elnaghy/laradate-filters

then publish config file

php artisan vendor:publish --provider="OmarElnaghy\LaraDateFilters\ServiceProvider"

Usage

Date Filtering Methods: Use the provided methods like FilterByDateRange, FilterByDateHoursRange, FilterByDateMinutesRange, etc., to filter records based on your specific criteria.


use Illuminate\Database\Eloquent\Model;
use OmarElnaghy\LaraDateFilters\Traits\Builder\PackageBuilder;

class YourModel extends Model
{
public function newEloquentBuilder(query) { return new PackageBuilder(query);
}
}

Features

Date Range Filtering: You can filter records based on a specified date range, including records created “after” or “before” a certain date and time.

$startDate = Carbon::parse('2023-9-03');
$range = \OmarElnaghy\LaraDateFilters\Enums\DateRange::INCLUSIVE;
$direction = 'after';                                                                                     
$results = Post::filterByDateRange(2,'day',$startDate, $direction, $range)->get();    

Flexible Time Units: You can filter records using various time units, such as seconds, minutes, hours, days, weeks, or months.

    $results = Post::filterByDateRange(2,'day',$startDate, $direction, $range)->get();      
    $results = Post::filterByDateRange(2,'week',$startDate, $direction, $range)->get();      
    $results = Post::filterByDateRange(2,'hour',$startDate, $direction, $range)->get();  

Inclusive or Exclusive Ranges:
You can choose whether the date range should be inclusive or exclusive, allowing you to fine-tune your query results.

$range = \OmarElnaghy\LaraDateFilters\Enums\DateRange::INCLUSIVE;
$range = \OmarElnaghy\LaraDateFilters\Enums\DateRange::EXCLUSIVE;

The key feature

The key feature of this trait is its ability to catch and handle dynamic date filtering methods based on a simple naming convention, making it incredibly convenient and powerful for developers:

Dynamic Date Filtering Magic

Unleash the magic of dynamic date filtering with our BuilderTrait! No more writing tedious, repetitive date filtering methods. With this trait, you can create date filters on the fly by simply following a naming convention.


    $results = Post::filterByDateSecondsRange(2,$startDate, $direction, $range)->get();
<span class="hljs-variable">$results</span> = Post::filterByDateMinutesRange(<span class="hljs-number">2</span>,<span class="hljs-variable">$startDate</span>, <span class="hljs-variable">$direction</span>, <span class="hljs-variable">$range</span>)-&gt;get();

<span class="hljs-variable">$results</span> = Post::filterByDateHoursRange(<span class="hljs-number">2</span>,<span class="hljs-variable">$startDate</span>, <span class="hljs-variable">$direction</span>, <span class="hljs-variable">$range</span>)-&gt;get();

<span class="hljs-variable">$results</span> = Post::filterByDateDaysRange(<span class="hljs-number">2</span>,<span class="hljs-variable">$startDate</span>, <span class="hljs-variable">$direction</span>, <span class="hljs-variable">$range</span>)-&gt;get();

<span class="hljs-variable">$results</span> = Post::filterByDateWeeksRange(<span class="hljs-number">2</span>,<span class="hljs-variable">$startDate</span>, <span class="hljs-variable">$direction</span>, <span class="hljs-variable">$range</span>)-&gt;get();

<span class="hljs-variable">$results</span> = Post::filterByDateMonthsRange(<span class="hljs-number">2</span>,<span class="hljs-variable">$startDate</span>, <span class="hljs-variable">$direction</span>, <span class="hljs-variable">$range</span>)-&gt;get();

Effortless Date Filtering

Imagine you want to filter records by date, but you don’t want to write separate methods for every possible duration—seconds, minutes, hours, days, weeks, months, and more. Our BuilderTrait has you covered. Just name your method following the pattern “filterByDateXRange,” and voila! X can be any duration, and the trait will handle the rest.

// [Duration] is a number referring to the number of [Date Unit] you want to search in          
return Post::filterByDate(Duration)(Date Unit)Range($startDate, $direction, $range)->get();

Examples

1 - Filter by Custom duration and date unit (second,minutes,hours,..)

startDate</span>=Carbon::parse(<spanclass="hljsstring">20230903</span>);<spanclass="hljsvariable">startDate</span> = Carbon::parse(<span class="hljs-string">'2023-09-03'</span>); <span class="hljs-variable">range = \OmarElnaghy\LaraDateFilters\Enums\DateRange::INCLUSIVE;
$direction = ‘after’;

return Post::filterByDate5DayRange(startDate</span>,<spanclass="hljsvariable">startDate</span>, <span class="hljs-variable">direction, $range)->get();

return Post::filterByDate6WeekRange(startDate</span>,<spanclass="hljsvariable">startDate</span>, <span class="hljs-variable">direction, $range)->get();

return Post::filterByDate7MonthRange(startDate</span>,<spanclass="hljsvariable">startDate</span>, <span class="hljs-variable">direction, $range)->get();

For more details visit GitHub.

Closing Notes

If you are a business and want to leverage Laravel for your next PHP-based web application, CRM Software, E-commerce application, SAAS application, or any custom web application development for your custom requirements, you must discover an amazing team with experience in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Please feel free to send us an email at rizwan@codebrisk.com or get in touch with us, our business person will get back to you.

Published at : 26-09-2023

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project