danieldewit/laravel-model-active

一个简单的特性,用于通过 'active' 布尔值来限定 Eloquent 模型

1.0.0 2017-10-23 13:41 UTC

This package is auto-updated.

Last update: 2024-09-17 18:31:10 UTC


README

Laravel 的一个特性,用于只拉取标记为 "active" 的模型。当特性应用于模型时,查询将默认只查找 active 列值为 1 的行。

要求

  • Laravel ^5.2

安装

将包添加到 composer.json 文件中

composer require daniel-de-wit/laravel-model-active

使用

向 Eloquent 模型添加 active 布尔字段

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddActiveModelSupportToArticleTable extends Migration
{
    public function up()
    {
        Schema::create('article', function (Blueprint $table) {
            $table->boolean('active')->default(true)->index();
        });
    }
    
    public function down()
    {
        Schema::table('article', function (Blueprint $table) {
            $table->dropColumn('active');
        });
    }
}

Active 特性添加到模型中

<?php

class MyModel extends Eloquent
{
    use Active;

    ...

}

移除 Active Scope

如果您想移除特定查询的 active 范围,可以使用 withoutGlobalScope 方法。

<?php

    MyModel::withoutGlobalScope(ActiveScope::class)->get();