定制 kesoji/laravel-removable-global-scopes 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kesoji/laravel-removable-global-scopes

最新稳定版本:v1.0.0

Composer 安装命令:

composer require kesoji/laravel-removable-global-scopes

包简介

Dynamically remove global scopes from Eloquent models in Laravel

README 文档

README

A Laravel package that adds the ability to dynamically remove global scopes from Eloquent models.

Installation

You can install the package via composer:

composer require kesoji/laravel-removable-global-scopes

Usage

Add the RemovableGlobalScopes trait to your Eloquent model:

use Illuminate\Database\Eloquent\Model;
use Kesoji\RemovableGlobalScopes\RemovableGlobalScopes;

class User extends Model
{
    use RemovableGlobalScopes;
    
    // Your model code...
}

Removing a Single Global Scope

You can remove a global scope by its name, class, or instance:

// Remove by name
User::removeGlobalScope('active');

// Remove by class
User::removeGlobalScope(ActiveScope::class);

// Remove by instance
User::removeGlobalScope(new ActiveScope);

Removing Multiple Global Scopes

Remove multiple global scopes at once:

User::removeGlobalScopes(['active', 'verified', TenantScope::class]);

Important Notes

⚠️ Warning: The removeGlobalScope() and removeGlobalScopes() methods permanently remove global scopes from the model class. This affects all subsequent queries on that model.

If you need to temporarily remove a global scope for a specific query, use Laravel's built-in withoutGlobalScope() method instead:

// Temporary removal for a single query
User::withoutGlobalScope('active')->get();

// Temporary removal of multiple scopes
User::withoutGlobalScopes(['active', 'verified'])->get();

Return Value

The removeGlobalScope() method returns the original global scopes array before removal, allowing you to potentially restore them later using Laravel's addGlobalScope() method:

// Remove and store original scopes
$originalScopes = User::removeGlobalScope('active');

// Do something without the scope...

// Restore if needed
if (isset($originalScopes[User::class]['active'])) {
    User::addGlobalScope('active', $originalScopes[User::class]['active']);
}

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-28