PHPackages                             maize-tech/laravel-tenant-aware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. maize-tech/laravel-tenant-aware

ActiveLibrary[Framework](/categories/framework)

maize-tech/laravel-tenant-aware
===============================

Laravel Tenant Aware

2.1.0(2y ago)141.1k4[4 PRs](https://github.com/maize-tech/laravel-tenant-aware/pulls)MITPHPPHP ^8.1CI passing

Since May 20Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/maize-tech/laravel-tenant-aware)[ Packagist](https://packagist.org/packages/maize-tech/laravel-tenant-aware)[ Docs](https://github.com/maize-tech/laravel-tenant-aware)[ GitHub Sponsors](https://github.com/maize-tech)[ RSS](/packages/maize-tech-laravel-tenant-aware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (12)Versions (8)Used By (0)

   ![Social Card of Laravel Tenant Aware](/art/socialcard-light.png)

Laravel Tenant Aware
====================

[](#laravel-tenant-aware)

[![Latest Version on Packagist](https://camo.githubusercontent.com/db5a31ac98d4b0f26a1ec5bf3d3ae2ad359c06b46d93ffb7649eacd16ec8fc04/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61697a652d746563682f6c61726176656c2d74656e616e742d61776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-tenant-aware)[![GitHub Tests Action Status](https://camo.githubusercontent.com/d99d0135ddd41f29e0a546346f792f32b5d9c66f11371934851bdefe74677016/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d74656e616e742d61776172652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-tenant-aware/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/422ee3e134db9659fcee3d43b08a337da3207bd6a68fe602b079f19f585a28f5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d74656e616e742d61776172652f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-tenant-aware/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/b295f1d8a1e1045970cac67cf534839ea7a3352d9d5d75309b7c49ffdab355e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61697a652d746563682f6c61726176656c2d74656e616e742d61776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-tenant-aware)

Easily integrate single-database multi tenant features into your Laravel application.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require maize-tech/laravel-tenant-aware
```

You can publish the config file with:

```
php artisan vendor:publish --tag="tenant-aware-config"
```

This is the contents of the published config file:

```
return [

    'tenant' => [

        /*
        |--------------------------------------------------------------------------
        | Tenant Model
        |--------------------------------------------------------------------------
        |
        | Here you may specify the fully qualified class name of the tenant model.
        |
        */

        'model' => null,

        /*
        |--------------------------------------------------------------------------
        | Tenant key name
        |--------------------------------------------------------------------------
        |
        | Here you may specify the column name for the tenant identifier.
        | All models using the multi-tenant feature should include this field
        | in their migration.
        |
        */

        'foreign_key_name' => 'tenant_id',

        /*
        |--------------------------------------------------------------------------
        | Actions
        |--------------------------------------------------------------------------
        |
        | Here you may specify the list of actions used to retrieve che current
        | and landlord tenants.
        |
        */

        'actions' => [
            'current' => Maize\TenantAware\Actions\TenantCurrentAction::class,
            'landlord' => Maize\TenantAware\Actions\TenantLandlordAction::class,
            'current_or_landlord' => Maize\TenantAware\Actions\TenantCurrentOrLandlordAction::class,
            'only_current' => Maize\TenantAware\Actions\TenantOnlyCurrentAction::class,
        ],

    ],

    'models' => [
        /*
        |--------------------------------------------------------------------------
        | Global Models
        |--------------------------------------------------------------------------
        |
        | Here you may specify the full list of global models who should return
        | all entities, including the ones related to the landlord tenant.
        |
        */

        'global' => [
            // App\Models\Article::class,
        ],

        'listen' => [

            /*
            |--------------------------------------------------------------------------
            | Set tenant key
            |--------------------------------------------------------------------------
            |
            | Here you may specify the action invoked when creating a multi-tenant
            | model entity.
            | By default, the action sets the tenant field to the current tenant key.
            |
            */

            'creating' => Maize\TenantAware\Listeners\SetTenantKey::class,
        ],
    ],

    'scope' => [

        /*
        |--------------------------------------------------------------------------
        | Scope apply
        |--------------------------------------------------------------------------
        |
        | Here you may override the default scope method applied to all models
        | who belong to a tenant.
        | The default class adds a where constraint to exclude entities not related
        | to the current user's tenant or to the landlord tenant.
        |
        */

        'apply' => Maize\TenantAware\Scopes\ScopeTenantAware::class,

        /*
        |--------------------------------------------------------------------------
        | Scope Methods
        |--------------------------------------------------------------------------
        |
        | Here you may override the default scope methods used to add the
        | where constraints to all models who belong to a tenant.
        |
        */

        'methods' => [
            Maize\TenantAware\Scopes\ScopeOrTenantWhere::class,
            Maize\TenantAware\Scopes\ScopeTenantWhere::class,
            Maize\TenantAware\Scopes\ScopeTenantAware::class,
        ],
    ],
];
```

Usage
-----

[](#usage)

### Configuration

[](#configuration)

Before getting started, make sure to fill in the `model` and `foreign_key_name` attributes under `config/tenant-aware.php`.

Also, don't forget to define your own `TenantCurrentAction` and `TenantLandlordAction` actions in order to retrieve both the current and landlord tenants.

For example, if you're using Spatie's [laravel-multitenancy](https://github.com/spatie/laravel-multitenancy) package your custom actions could look like this:

```
