All Projects → orchestral → Tenanti

orchestral / Tenanti

Licence: mit
[Package] Multi-tenant Database Schema Manager for Laravel

Projects that are alternatives of or similar to Tenanti

Bagisto
An easy to use, free and open source laravel eCommerce platform to build your online shop in no time.
Stars: ✭ 4,140 (+688.57%)
Mutual labels:  hacktoberfest, laravel
Comments
Native comments for your Laravel application.
Stars: ✭ 390 (-25.71%)
Mutual labels:  hacktoberfest, laravel
Laravel Modules
Module Management In Laravel
Stars: ✭ 3,910 (+644.76%)
Mutual labels:  hacktoberfest, laravel
Finance
A self hosted app to help you get a better understanding of your personal finances.
Stars: ✭ 313 (-40.38%)
Mutual labels:  hacktoberfest, laravel
Laravel 8 Simple Cms
Laravel 8 content management system for starters.
Stars: ✭ 444 (-15.43%)
Mutual labels:  hacktoberfest, laravel
Corcel
Use WordPress backend with Laravel or any PHP application
Stars: ✭ 3,504 (+567.43%)
Mutual labels:  hacktoberfest, laravel
Collision
💥 Collision is a beautiful error reporting tool for command-line applications
Stars: ✭ 3,993 (+660.57%)
Mutual labels:  hacktoberfest, laravel
Short Url
A Laravel package for creating shortened URLs for your web apps.
Stars: ✭ 293 (-44.19%)
Mutual labels:  hacktoberfest, laravel
Freek.dev
The sourcecode of freek.dev
Stars: ✭ 407 (-22.48%)
Mutual labels:  hacktoberfest, laravel
Laravel Acl
This package helps you to associate users with permissions and permission groups with laravel framework
Stars: ✭ 404 (-23.05%)
Mutual labels:  hacktoberfest, laravel
Laravel Starter
A CMS like modular starter application project built with Laravel 8.x.
Stars: ✭ 299 (-43.05%)
Mutual labels:  hacktoberfest, laravel
Laravel Medialibrary
Associate files with Eloquent models
Stars: ✭ 4,743 (+803.43%)
Mutual labels:  hacktoberfest, laravel
Providers
A Collection of Providers for Laravel Socialite
Stars: ✭ 301 (-42.67%)
Mutual labels:  hacktoberfest, laravel
Validator Docs
Validação de CPF, CNPJ, CNH, NIS, Título Eleitoral e Cartão Nacional de Saúde com Laravel.
Stars: ✭ 334 (-36.38%)
Mutual labels:  hacktoberfest, laravel
Laravel Api Boilerplate
Laravel API Boilerplate | Please consult the Wiki !
Stars: ✭ 300 (-42.86%)
Mutual labels:  hacktoberfest, laravel
Larasail
LaraSail - Set Sail with your Laravel app on DigitalOcean
Stars: ✭ 348 (-33.71%)
Mutual labels:  hacktoberfest, laravel
Tlint
Tighten linter for Laravel conventions
Stars: ✭ 274 (-47.81%)
Mutual labels:  hacktoberfest, laravel
Spatie.be
The source code of spatie.be
Stars: ✭ 274 (-47.81%)
Mutual labels:  hacktoberfest, laravel
Laravel Datatables
jQuery DataTables API for Laravel 4|5|6|7|8
Stars: ✭ 4,134 (+687.43%)
Mutual labels:  hacktoberfest, laravel
Telegram
✈️ Telegram Notifications Channel for Laravel
Stars: ✭ 450 (-14.29%)
Mutual labels:  hacktoberfest, laravel

Multi-tenant Database Schema Manager for Laravel

Tenanti allow you to manage multi-tenant data schema and migration manager for your Laravel application.

tests Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

Table of Content

Version Compatibility

Laravel Tenanti
5.5.x 3.5.x
5.6.x 3.6.x
5.7.x 3.7.x
5.8.x 3.8.x
6.x 4.x
7.x 5.x

Installation

To install through composer, run the following command from terminal:

composer require "orchestra/tenanti"

Configuration

Next add the following service provider in config/app.php.

'providers' => [

    // ...
    Orchestra\Tenanti\TenantiServiceProvider::class,
    Orchestra\Tenanti\CommandServiceProvider::class,
],

The command utility is enabled via Orchestra\Tenanti\CommandServiceProvider.

Aliases

To make development easier, you could add Orchestra\Support\Facades\Tenanti alias for easier reference:

'aliases' => [

    'Tenanti' => Orchestra\Support\Facades\Tenanti::class,

],

Publish Configuration

To make it easier to configuration your tenant setup, publish the configuration:

php artisan vendor:publish

Usage

Configuration Tenant Driver for Single Database

Open config/orchestra/tenanti.php and customize the drivers.

<?php

return [
    'drivers' => [
        'user' => [
            'model' => App\User::class,
            'paths' => [
                database_path('tenanti/user'),
            ],
            'shared' => true,
        ],
    ],
];

You can customize, or add new driver in the configuration. It is important to note that model configuration only work with Eloquent instance.

Setup migration autoload

For each driver, you should also consider adding the migration path into autoload (if it not already defined). To do this you can edit your composer.json.

composer.json
{
    "autoload": {
        "classmap": [
            "database/tenant/users"
        ]
    }
}

Setup Tenantor Model

Now that we have setup the configuration, let add an observer to our User class:

<?php 

namespace App;

use App\Observers\UserObserver;
use Orchestra\Tenanti\Tenantor;
use Illuminate\Notifications\Notifiable;
use Orchestra\Tenanti\Contracts\TenantProvider;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements TenantProvider
{
    use Notifiable;

    /**
     * Convert to tenantor.
     * 
     * @return \Orchestra\Tenanti\Tenantor
     */
    public function asTenantor(): Tenantor
    {
        return Tenantor::fromEloquent('user', $this);
    }

    /**
     * Make a tenantor.
     *
     * @return \Orchestra\Tenanti\Tenantor
     */
    public static function makeTenantor($key, $connection = null): Tenantor
    {
        return Tenantor::make(
            'user', $key, $connection ?: (new static())->getConnectionName()
        );
    }

    /**
     * The "booting" method of the model.
     */
    protected static function boot()
    {
        parent::boot();

        static::observe(new UserObserver);
    }
}

and your App\Observers\UserObserver class should consist of the following:

<?php 

namespace App\Observers;

use Orchestra\Tenanti\Observer;

class UserObserver extends Observer
{
    public function getDriverName()
    {
        return 'user';
    }
}

Console Support

Tenanti include additional command to help you run bulk migration when a new schema is created, the available command resemble the usage available from php artisan migrate namespace.

Command Description
php artisan tenanti:install {driver} Setup migration table on each entry for a given driver.
php artisan tenanti:make {driver} {name} Make a new Schema generator for a given driver.
php artisan tenanti:migrate {driver} Run migration on each entry for a given driver.
php artisan tenanti:rollback {driver} Rollback migration on each entry for a given driver.
php artisan tenanti:reset {driver} Reset migration on each entry for a given driver.
php artisan tenanti:refresh {driver} Refresh migration (reset and migrate) on each entry for a given driver.
php artisan tenanti:queue {driver} {action} Execute any of above action using separate queue to minimize impact on current process.
php artisan tenanti:tinker {driver} {id} Run tinker using a given driver and ID.

Multi Database Connection Setup

Instead of using Tenanti with a single database connection, you could also setup a database connection for each tenant.

Configuration Tenant Driver for Multiple Database

Open config/orchestra/tenanti.php and customize the drivers.

<?php

return [
    'drivers' => [
        'user' => [
            'model'  => App\User::class,
            'path'   => database_path('tenanti/user'),
            'shared' => false,
        ],
    ],
];

By introducing a migration config, you can now setup the migration table name to be tenant_migrations instead of user_{id}_migrations.

Database Connection Resolver

For tenanti to automatically resolve your multiple database connection, we need to setup the resolver. You can do this via:

<?php namespace App\Providers;

use Orchestra\Support\Facades\Tenanti;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Tenanti::connection('tenants', function (User $entity, array $config) {
            $config['database'] = "acme_{$entity->getKey()}"; 
            // refer to config under `database.connections.tenants.*`.

            return $config;
        });
    }
}

Behind the scene, $config will contain the template database configuration fetch from "database.connections.tenants" (based on the first parameter tenants). We can dynamically modify the connection configuration and return the updated configuration for the tenant.

Setting Default Database Connection

Alternatively you can also use Tenanti to set the default database connection for your application:

use App\User;
use Orchestra\Support\Facades\Tenanti;

// ...

$user = User::find(5);

Tenanti::driver('user')->asDefaultConnection($user, 'tenants_{id}');

Most of the time, this would be use in a Middleware Class when you resolve the tenant ID based on Illuminate\Http\Request object.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].