codekj/laravel-date-scopes

为 Laravel Eloquent 模型提供日期范围

1.0.1 2020-08-06 21:30 UTC

This package is auto-updated.

Last update: 2024-09-07 06:48:49 UTC


README

为 Laravel Eloquent 模型提供日期范围

要求

  • Composer
  • Laravel 或 Lumen
  • PHP >= 7.1

安装

安装 composer 包

composer require codekj/laravel-date-scopes

用法

在模型中使用 DateScopes 特性

<?php

namespace App;

use CodeKJ\Laravel\Traits\DateScopes\DateScopes;

class User extends Model
{
    use DateScopes;

    /* ... */
}

方法

默认日期字段: created_at. 所有方法都可以有自定义日期字段参数,例如 today('updated_at')

示例

// Get records only from today
$todayUsers = User::today()->get();
// Get records from last minute (previous minute)
// Example: if time is 10:30, you will get records ONLY from 10:29
$lastMinuteUsers = User::lastMinute()->get();
// Get records from an hour ago until this moment
// Example: if time is 10:30, you will get records from 09:30 until 10:30
$hourAgoUsers = User::hourAgo()->get();
// Get records from 2 weeks ago until this moment
// Example: if date is 2020-08-30 10:30:00, you will get records from 2020-08-16 10:30:00 until 2020-08-30 10:30:00
$weeksAgoUsers = User::weeksAgo(2)->get();

许可证

MIT 许可证 (MIT)