frameck / laravel-query-date-helpers
这是我开发的laravel-query-date-helpers包
v1.0.1
2023-05-08 19:17 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
This package is auto-updated.
Last update: 2024-09-09 17:17:04 UTC
README
本包提供了一些关于日期的有用查询和Eloquent构建器宏。
安装
您可以通过composer安装此包
composer require frameck/laravel-query-date-helpers
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="laravel-query-date-helpers-config"
这是已发布配置文件的内容
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.net.cn/docs/10.x/eloquent#timestamps * * When using Query Builder it uses the column below */ 'column' => 'created_at', ];
使用方法
- 使用
'register_macros' => false
use Frameck\LaravelQueryDateHelpers\Traits\HasDateScopes; class Order extends Model { use HasFactory; use HasDateScopes; }
- 使用
'register_macros' => true
由于宏已直接在Eloquent和查询构建器上注册,因此无需任何额外配置。如果您选择此选项,您可以从模型类或从DB
外观使用以下列出的所有函数。
本节中的所有示例都假设日期为2023年5月8日。每个方法都有column
参数作为倒数第二个参数,因此您可以使用两种方式使用它
Order::weekToDate(now()->subYear(), 'date')
传递所有参数Order::weekToDate(column: 'date')
使用命名参数
列默认值
- 使用
QueryBuilder
时为created_at
- 使用
EloquentBuilder
时,它获取模型上设置的CREATED_AT
,如果找不到则默认为配置中设置的column
值
基本方法
昨天
今天
明天
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`) <= '2023-07-08' // using query builder on DB facade DB::table('orders')->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-07-08'
至日期
weekToDate
monthToDate
quarterToDate
yearToDate
// all toDate methods accept the following parameters ?Carbon $date = null, ?string $column = null, ?DateRangeType $dateRangeType = null // basic usage Order::weekToDate(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-05-08' and date(`orders`.`created_at`) <= '2023-05-08' // basic usage on a custom column Order::weekToDate(column: 'date'); // select * from `orders` where date(`orders`.`date`) >= '2023-05-08' and date(`orders`.`date`) <= '2023-05-08' // or you can pass a Carbon instance for a custom date Order::weekToDate(now()->subYear(), 'date'); // select * from `orders` where date(`orders`.`date`) >= '2022-05-02' and date(`orders`.`date`) <= '2022-05-08'
最后
最后分钟
最后小时
最后一周
最后一个月
最后一个季度
最后一年
// all last methods accept the following parameters ?string $column = null, ?DateRangeType $dateRangeType = null Order::lastHour(); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 19:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' Order::lastMonth(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-04-01' and date(`orders`.`created_at`) <= '2023-04-30' Order::lastQuarter(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-01-01' and date(`orders`.`created_at`) <= '2023-03-31'
最后N
lastMinutes
默认为5分钟lastHours
默认为2小时lastDays
默认为7天lastWeeks
默认为2周lastMonths
默认为2个月lastQuarters
默认为2个季度lastYears
默认为2年
// all last n methods accept the following parameters int $numberOfMinutes = 5, ?Carbon $date = null, ?string $column = null, ?DateRangeType $dateRangeType = null // basic usage Order::lastHours(); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 13:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' Order::lastMonths(); // select * from `orders` where `orders`.`created_at` >= '2023-03-08 20:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' Order::lastQuarters(); // select * from `orders` where `orders`.`created_at` >= '2022-11-08 20:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' // more complex usage Order::lastHours(12); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 08:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' Order::lastMonths(6); // select * from `orders` where `orders`.`created_at` >= '2022-11-08 20:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' Order::lastQuarters(3); // select * from `orders` where `orders`.`created_at` >= '2022-08-08 20:46:38' and `orders`.`created_at` <= '2023-05-08 20:46:38' // even more complex usage Order::lastHours(12, now()->subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 08:46:38' and `orders`.`created_at` <= '2022-05-08 20:46:38' Order::lastMonths(6, now()->subYear()); // 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()); // select * from `orders` where `orders`.`created_at` >= '2021-08-08 20:46:38' and `orders`.`created_at` <= '2022-05-08 20:46:38' // passing a DateRangeType (when DateRangeType::EXCLUSIVE the <= becomes <) Order::lastHours(12, now()->subYear(), dateRangeType: DateRangeType::EXCLUSIVE); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 08:46:38' and `orders`.`created_at` < '2022-05-08 20:46:38' Order::lastMonths(6, now()->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'
这个
这个星期
这个月
这个季度
今年
// 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-14' Order::thisMonth(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-05-01' and date(`orders`.`created_at`) <= '2023-05-31' Order::thisQuarter(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-04-01' and date(`orders`.`created_at`) <= '2023-06-30'
下一个
下分钟
下小时
下个星期
下个月
下个季度
明年
// all next methods accept the following parameters ?string $column = null, ?DateRangeType $dateRangeType = null Order::nextHour(); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2023-05-08 21:46:38' Order::nextMonth(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-06-01' and date(`orders`.`created_at`) <= '2023-06-30' Order::nextQuarter(); // select * from `orders` where date(`orders`.`created_at`) >= '2023-07-01' and date(`orders`.`created_at`) <= '2023-09-30'
下一个N
nextMinutes
默认为5分钟nextHours
默认为2小时nextDays
默认为7天nextWeeks
默认为2周nextMonths
默认为2个月nextQuarters
默认为2个季度nextYears
默认为2年
// all next n methods accept the following parameters int $numberOfMinutes = 5, ?Carbon $date = null, ?string $column = null, ?DateRangeType $dateRangeType = null // basic usage Order::nextHours(); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2023-05-09 03:46:38' Order::nextMonths(); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2023-07-08 20:46:38' Order::nextQuarters(); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2023-11-08 20:46:38' // more complex usage Order::nextHours(12); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2023-05-09 08:46:38' Order::nextMonths(6); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2023-11-08 20:46:38' Order::nextQuarters(3); // select * from `orders` where `orders`.`created_at` >= '2023-05-08 20:46:38' and `orders`.`created_at` <= '2024-02-08 20:46:38' // even more complex usage Order::nextHours(12, now()->subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 20:46:38' and `orders`.`created_at` <= '2022-05-09 08:46:38' Order::nextMonths(6, now()->subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 20:46:38' and `orders`.`created_at` <= '2022-11-08 20:46:38' Order::nextQuarters(3, now()->subYear()); // select * from `orders` where `orders`.`created_at` >= '2022-05-08 20:46:38' and `orders`.`created_at` <= '2023-02-08 20:46:38'
测试
composer test
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请查看我们的安全策略以了解如何报告安全漏洞。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。