codekj/laravel-date-scopes

Provides date scopes for Laravel Eloquent models

1.0.1 2020-08-06 21:30 UTC

This package is auto-updated.

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


README

Provides date scopes for Laravel Eloquent models

Requirements

  • Composer
  • Laravel or Lumen
  • PHP >= 7.1

Installation

Install composer package

composer require codekj/laravel-date-scopes

Usage

Use DateScopes trait in model

<?php

namespace App;

use CodeKJ\Laravel\Traits\DateScopes\DateScopes;

class User extends Model
{
    use DateScopes;

    /* ... */
}

Methods

Default date field: created_at. All methods can have custom date field parameter, example today('updated_at')

Examples

// 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();

License

The MIT License (MIT)