sofa/eloquent-scopes

此包已被废弃且不再维护。未建议替代包。

为Laravel的Eloquent查询构建器提供便捷的作用域。

v1.0.3 2015-09-16 17:24 UTC

This package is auto-updated.

Last update: 2021-04-07 07:59:56 UTC


README

为Eloquent (Laravel)查询构建器提供便捷的作用域。

安装

包需要 PHP 5.4+ 并与 Laravel 5/5.1 兼容。

  1. 在您的 composer.json 中引入包

        "require": {
            ...
            "sofa/eloquent-scopes": "~1.0",
        },
    
  2. 将特质添加到您的类中,例如: use \Sofa\EloquentScopes\PeriodScopes;

使用示例

PeriodScopes - 提供了在给定范围内轻松获取记录的方法,相对于NOW。

class Subscription extends Model
{
    use PeriodScopes;

    // optionally you may provide the column to be filtered
    //   By default self::CREATED_AT -> 'created_at' will be used
    const PERIOD_COLUMN = 'expires_at';
}

class Subscription extends Model
{
    use PeriodScopes;
}
// Given it's September 11th, 2015

// count users created in August
User::lastMonth()->count();

// get users created on September 10th
User::yesterday()->get();

// count users who logged-in in 2014 & 2015
User::periods('year', 1, 'last_login', true)->count();

// count users created in 2014 & 2015
User::periods('year', -1, null, true)->count();
// or
User::periods('year', -1, true)->count();

// Get subscriptions expiring in October
User::nextMonth()->get();

// Get subscriptions expired in past 7 days
User::periods('day', -7)->get();

// Get subscriptions expiring in next 30 days
User::periods('day', 30)->get();


//
// Obviously these are query extensions, so you can chain them however you like
// 
User::query()->tomorrow()->get();
User::where(...)->tomorrow()->get();
(new User)->tomorrow()->get();

路线图

待定