syntech/globalscopes

A Laravel package to add general model scopes to all models

2.0.0 2024-10-27 13:02 UTC

This package is auto-updated.

Last update: 2025-05-07 11:56:43 UTC


README

A Laravel package for adding global model scopes and attributes without modifying individual models.

Installation

You can install the package via Composer:

composer require syntech/globalscopes

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Syntech\GlobalScopes\GlobalScopesServiceProvider"

This will create a globalscopes.php configuration file in the config directory of your Laravel application.

Example Configuration

return [

       /*
    |--------------------------------------------------------------------------
    | Global Query Methods
    |--------------------------------------------------------------------------
    |
    | Define any global query methods that should be available across all
    | Eloquent models. These methods can be used to add commonly used query
    | functionality to all models.
    |
    | Example:
    | 'active' => function () {
    |    return $this->where('status', 1);
    | },
    |
    */

    'methods' => [
        'active' => function () {
            return $this->where('status', 1);
        },
        'someMore' => function () {
                    // add more ...
            }
    ],
 
];

Usage

Adding Global Scopes

Once configured, the global scopes will automatically be applied to all Eloquent models. Adding Global Attributes

Global attributes can be accessed as if they are defined directly on the model:

$customers = Customer::active()->get();

return $customers; // Uses the global attribute logic