lukapeharda / multitenancy by lpeharda

A single DB multitenancy package for Laravel based on subdomain routing.
16
1
2
Package Data
Maintainer Username: lpeharda
Maintainer Contact: luka.peharda@gmail.com (Luka Peharda)
Package Create Date: 2017-01-21
Package Last Update: 2017-01-26
Language: PHP
License: MIT
Last Refreshed: 2024-04-25 03:01:41
Package Statistics
Total Downloads: 16
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

MultiTenancy

Introduction

MultiTenancy is a Laravel single DB multitenancy package based on subdomain routing.

Installation

Require this package, with Composer, in the root directory of your project.

composer require lukapeharda/multitenancy

Add the service provider to config/app.php in the providers array.

LukaPeharda\MultiTenancy\Providers\ContextServiceProvider::class,

Publish the config file and modify its params to fit your needs and installation.

php artisan vendor:publish --provider="LukaPeharda\MultiTenancy\Providers\ContextServiceProvider"

Usage

Add the LukaPeharda\MultiTenancy\Scopes\Contextable trait to your models that are "tenant" dependable. This trait will automatically load global scope which will filter all your queries by defined tenant key.

Beside adding trait to your models, your model DB schema needs to have the tenant key as its attribute (and most likely as its foreign key).

To disable tenant global scope use withoutGlobalScope builder method:

$model->withoutGlobalScope(\LukaPeharda\MultiTenancy\Scopes\TenantScope::class);

Fetching the current tenant (and all of its attributes) is available through helper function context.

// To fetch entire tenant object
$tenant = context();

// To fetch one of its attribute
$tenantId = context('id');

License

MultiTenancy is open-sourced software licensed under the MIT license