maize-tech/laravel-excludable

Laravel Excludable

4.1.0 2024-03-27 12:05 UTC

This package is auto-updated.

Last update: 2024-09-08 05:50:24 UTC


README

Laravel Excludable

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

轻松排除模型实体从 eloquent 查询。

此包允许您定义一个模型实体子集,该子集应从 eloquent 查询中排除。您将能够覆盖默认的 Exclusion 模型和相关的迁移,从而最终通过定义应有效排除子集的实体来限制排除上下文。

一个示例用法可以是具有多租户场景和一组全局实体应用程序。虽然这些实体应由所有租户访问,但其中一些可能希望为他们的用户隐藏这些实体的子集。您可以在用法部分找到示例。

安装

您可以通过 composer 安装此包

composer require maize-tech/laravel-excludable

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --provider="Maize\Excludable\ExcludableServiceProvider" --tag="excludable-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Maize\Excludable\ExcludableServiceProvider" --tag="excludable-config"

这是已发布配置文件的内容

return [

    /*
    |--------------------------------------------------------------------------
    | Exclusion model
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the exclusion model.
    |
    */

    'exclusion_model' => Maize\Excludable\Models\Exclusion::class,
    
    /*
    |--------------------------------------------------------------------------
    | Has exclusion query
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the exclusion query.
    |
    */

    'has_exclusion_query' => Maize\Excludable\Queries\HasExclusionQuery::class,
];

用法

基本

要使用此包,请将 Maize\Excludable\Excludable 特性添加到所有想要使其可排除的模型中。

以下是一个包含 Excludable 特性的示例模型

<?php

namespace App\Models;

use Maize\Excludable\Excludable;

class Article extends Model
{
    use Excludable;

    protected $fillable = [
        'title',
        'body',
    ];
}

现在,您可以查询特定的文章实体并将其标记为排除。

use App\Models\Article;

$article = Article::query()->findOrFail(1)

$article->addToExclusion();

$article->excluded(); // returns true

就这些!

此包将给定实体添加到排除表中,因此所有与文章相关的查询都将排除它。

use App\Models\Article;

Article::findOrFail(1); // throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException

排除所有模型实体

要排除特定模型的全部实体,可以使用 excludeAllModels 方法

use App\Models\Article;

Article::excludeAllModels();

Article::query()->count(); // returns 0

给定方法将创建一个具有通配符的排除实体,这意味着所有新创建的实体也将被排除。

您还可以提供不应排除的实体子集

use App\Models\Article;

$article = Article::query()->find(1);

Article::excludeAllModels($article); // passing a model entity

Article::query()->count(); // returns 1
use App\Models\Article;

Article::excludeAllModels([1,2,3]); // passing the model keys

Article::query()->count(); // returns 3

要检查特定模型是否具有通配符,您可以使用 hasExclusionWildcard 方法

use App\Models\Article;

Article::excludeAllModels();

$hasWildcard = Article::hasExclusionWildcard(); // returns true

包含所有模型实体

要重新包含特定模型的全部实体,可以使用 includeAllModels 方法

use App\Models\Article;

Article::includeAllModels();

Article::query()->count(); // returns 0

给定方法将删除与文章模型相关的所有排除实体。

包含排除的实体

use App\Models\Article;

Article::withExcluded()->get(); // queries all models, including those marked as excluded 

仅显示排除的实体

use App\Models\Article;

Article::onlyExcluded()->get(); // queries only excluded entities

事件处理

在排除实体时,该包会自动抛出两个单独的事件

  • excluding,在实体实际排除之前抛出。这可以用于观察者,例如,监听此事件并对相关实体执行某种“验证”。如果给定的验证失败,您可以简单地返回 false,实体将不会被排除;
  • excluded,在实体被标记为排除后立即抛出。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

有关报告安全漏洞的详细信息,请参阅 我们的安全策略

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件