PHPackages                             frameck/laravel-query-date-helpers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. frameck/laravel-query-date-helpers

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

frameck/laravel-query-date-helpers
==================================

This is my package laravel-query-date-helpers

v1.0.1(3y ago)014[3 PRs](https://github.com/Frameck/laravel-query-date-helpers/pulls)MITPHPPHP ^8.1

Since Apr 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Frameck/laravel-query-date-helpers)[ Packagist](https://packagist.org/packages/frameck/laravel-query-date-helpers)[ Docs](https://github.com/frameck/laravel-query-date-helpers)[ RSS](/packages/frameck-laravel-query-date-helpers/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (8)Versions (8)Used By (0)

Laravel Query Date Helpers
==========================

[](#laravel-query-date-helpers)

[![Latest Version on Packagist](https://camo.githubusercontent.com/39cfc8d4838471c10ce6f995485c748ab0c9d201a6eedf73553b892c6ed41fbf/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6672616d65636b2f6c61726176656c2d71756572792d646174652d68656c706572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/frameck/laravel-query-date-helpers)[![GitHub Tests Action Status](https://camo.githubusercontent.com/202697bfaebdfb3e6a79811ce372bb36dd1aa804de0cb6efcd871c6f2621cd50/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6672616d65636b2f6c61726176656c2d71756572792d646174652d68656c706572732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/frameck/laravel-query-date-helpers/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/dbf3002ae40d77b58aa460810fb33fa8454f44b387b7f63831313ea33e427edb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6672616d65636b2f6c61726176656c2d71756572792d646174652d68656c706572732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/frameck/laravel-query-date-helpers/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/59fac159f933ded0460044083ffea3eec2de936fb1a2dbdb5ed378bf585e736c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6672616d65636b2f6c61726176656c2d71756572792d646174652d68656c706572732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/frameck/laravel-query-date-helpers)

This package provides some useful query and eloquent builder macros regarding dates.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require frameck/laravel-query-date-helpers
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-query-date-helpers-config"
```

This is the contents of the published config file:

```
use Frameck\LaravelQueryDateHelpers\Enums\DateRangeType;

return [
    /**
     * If you want to exclude the current day/week/month/year etc. in the range you could use the exclusive range here as a default.
     * Note that you can also optionally specify it for almost every macro/scope directly when using it:
     * Order::lastDays(range: DateRangeType::EXCLUSIVE);
     * This will do an exclusive query, even though the global default range here is set to inclusive.
     *
     * Possible values here are: DateRangeType::INCLUSIVE or DateRangeType::EXCLUSIVE
     */
    'date_range_type' => DateRangeType::INCLUSIVE,

    /**
     * By default this package registers all the provided macros on Eloquent and Query builder.
     * If you don't want this behaviour set this value to false.
     * If you decide to not use the macros, this package provides also a trait that you can use on a specific model.
     */
    'register_macros' => true,

    /**
     * When using Eloquent the package will use the CREATED_AT column
     *
     * @link https://laravel.com/docs/10.x/eloquent#timestamps
     *
     * When using Query Builder it uses the column below
     */
    'column' => 'created_at',
];
```

Usage
-----

[](#usage)

1. Using `'register_macros' => false`

```
use Frameck\LaravelQueryDateHelpers\Traits\HasDateScopes;

class Order extends Model
{
    use HasFactory;
    use HasDateScopes;
}
```

2. Using `'register_macros' => true`You don't you need any additional configuration since the macro are registered directly on the Eloquent and Query builder. If you choose this option you can use all the functions listed below either querying from the model class or from the `DB` facade.

All the examples in this section assume that the date is May 8, 2023. Every method has a `column` parameter as its second to last, so you can use it in two ways:

1. `Order::weekToDate(now()->subYear(), 'date')` passing all parameters
2. `Order::weekToDate(column: 'date')` with named parameter

Column default:

- when using `QueryBuilder` is `created_at`
- when using `EloquentBuilder` it gets the `CREATED_AT` set on the model, and if it doesn't find one it defaults to the `column` value set in the config

#### BASIC METHODS

[](#basic-methods)

- `yesterday`
- `today`
- `tomorrow`
- `betweenDates`

```
// yesterday, today and tomorrow methods accept the following parameters
?string $column = null

// betweenDates method accept the following parameters
?Carbon $dateStart = null,
?Carbon $dateEnd = null,
?string $column = null,
?DateRangeType $dateRangeType = null

// betweenDates method accepts these parameters

// using eloquent builder on Order model
Order::yesterday(); // select * from `orders` where date(`orders`.`created_at`) = '2023-05-07'
Order::today(); // select * from `orders` where date(`orders`.`created_at`) = '2023-05-08'
Order::tomorrow(); // select * from `orders` where date(`orders`.`created_at`) = '2023-05-09'
Order::betweenDates(now()->startOfMonth(), now()->addMonths(2)); // select * from `orders` where date(`orders`.`created_at`) >= '2023-05-01' and date(`orders`.`created_at`) yesterday()->toRawSql(); // select * from `orders` where date(`created_at`) = '2023-05-07'
DB::table('orders')->today()->toRawSql(); // select * from `orders` where date(`created_at`) = '2023-05-08'
DB::table('orders')->tomorrow()->toRawSql(); // select * from `orders` where date(`created_at`) = '2023-05-09'
DB::table('orders')->betweenDates(now()->startOfMonth(), now()->addMonths(2))->toRawSql(); // select * from `orders` where date(`created_at`) >= '2023-05-01' and date(`created_at`) = '2023-05-08' and date(`orders`.`created_at`) = '2023-05-08' and date(`orders`.`date`) subYear(), 'date'); // select * from `orders` where date(`orders`.`date`) >= '2022-05-02' and date(`orders`.`date`) = '2023-05-08 19:46:38' and `orders`.`created_at` = '2023-04-01' and date(`orders`.`created_at`) = '2023-01-01' and date(`orders`.`created_at`) = '2023-05-08 13:46:38' and `orders`.`created_at` = '2023-03-08 20:46:38' and `orders`.`created_at` = '2022-11-08 20:46:38' and `orders`.`created_at` = '2023-05-08 08:46:38' and `orders`.`created_at` = '2022-11-08 20:46:38' and `orders`.`created_at` = '2022-08-08 20:46:38' and `orders`.`created_at` subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 08:46:38' and `orders`.`created_at` subYear()); // select * from `orders` where `orders`.`created_at` >= '2021-11-08 20:46:38' and `orders`.`created_at` subYear()); // select * from `orders` where `orders`.`created_at` >= '2021-08-08 20:46:38' and `orders`.`created_at` subYear(), dateRangeType: DateRangeType::EXCLUSIVE); // select * from `orders` where `orders`.`created_at` >= '2021-11-08 20:46:38' and `orders`.`created_at` < '2022-05-08 20:46:38'
Order::lastQuarters(3, now()->subYear(), dateRangeType: DateRangeType::EXCLUSIVE); // select * from `orders` where `orders`.`created_at` >= '2021-08-08 20:46:38' and `orders`.`created_at` < '2022-05-08 20:46:38'
```

#### THIS

[](#this)

- `thisWeek`
- `thisMonth`
- `thisQuarter`
- `thisYear`

```
// all this methods accept the following parameters
?string $column = null,
?DateRangeType $dateRangeType = null

Order::thisWeek(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-05-08' and date(`orders`.`created_at`) = '2023-05-01' and date(`orders`.`created_at`) = '2023-04-01' and date(`orders`.`created_at`) = '2023-05-08 20:46:38' and `orders`.`created_at` = '2023-06-01' and date(`orders`.`created_at`) = '2023-07-01' and date(`orders`.`created_at`) = '2023-05-08 20:46:38' and `orders`.`created_at` = '2023-05-08 20:46:38' and `orders`.`created_at` = '2023-05-08 20:46:38' and `orders`.`created_at` = '2023-05-08 20:46:38' and `orders`.`created_at` = '2023-05-08 20:46:38' and `orders`.`created_at` = '2023-05-08 20:46:38' and `orders`.`created_at` subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 20:46:38' and `orders`.`created_at` subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 20:46:38' and `orders`.`created_at` subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 20:46:38' and `orders`.`created_at`
