wowe/laravel-eloquent-scopes

一些Eloquent的通用范围

v3.0.0 2017-05-19 01:43 UTC

This package is auto-updated.

Last update: 2024-09-06 09:17:39 UTC


README

此包包含一些Laravel Eloquent的有用全局范围。

安装
特质
用法
发行说明
版本兼容性
许可

安装

通过Composer

composer require wowe/laravel-eloquent-scopes

特质

ActivatableTrait

当这个特质添加到Eloquent模型中时,它将仅返回将active字段设置为true的行。

如果您想使用不同于active的字段名,请在Eloquent模型上设置一个名为ACTIVE的常量,并使用您希望使用的任何名称。

此特质包含以下方法

  • deactivate:将active字段设置为false
  • activate:将active字段设置为true
  • withInactive:将在结果中包括所有条目,无论active字段的值如何。
  • onlyInactive:仅包括那些active字段设置为false的条目。

用法

将特质添加到模型

ActivatableTrait添加到Eloquent模型中。

use Wowe\Eloquent\Scopes\ActivatableTrait;

class Test extends \Eloquent
{
    use ActivatableTrait;

    const ACTIVE = 'is_active';
}

查询模型

// Get all entries that are active
Test::all();

// Get a single entry only if active
Test::find(1);

// Get all entries
Test::withInactive()->get();

// Get all inactive entries
Test::inactiveOnly()->get();

版本兼容性

许可

此软件是开源软件,许可协议为MIT许可