2017 © Pedro Peláez
 

library laravel-multi-db-tenant

This package is used to create and manage laravel multitenant applications

image

rndwiga/laravel-multi-db-tenant

This package is used to create and manage laravel multitenant applications

  • Thursday, February 2, 2017
  • by Rndwiga
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 4 Versions
  • 33 % Grown

The README.md

Build Status, (*1)

Multi-Database Tenant Package for Laravel

This package is meant for Laravel applications to help with Multi-Database Tenant Applications., (*2)

A sample laravel application that uses this package can be found at github, (*3)

Installation

composer require rndwiga\MultiTenant

Add the service providers to your application providers array in config/app.php A Facade is also available and can be added..., (*4)

Providers

rndwiga\MultiTenant\LaraMultiDbTenantServiceProvider::class

Facade

'LaraMultiDbTenant' => rndwiga\MultiTenant\Facade::class

Configuration Files

php artisan vendor:publish

This will copy the laramultidbtenant.php configuration file into the app/config directory as well as copy the tenant, tenant_users and tenant_databases migration files needed. Create your models., (*5)

Setup the database for your Tenants in your .env files, (*6)

Create the needed model classes

This will create the Tenant, TenantDatabase, TenantUser models. Make sure to provide the right relationships and don't forget to create the relationships within the User, Tenant, TenantUser and TenantDatabase models as well as set the connection properties., (*7)

php artisan tenant:basemodels
class Tenant extends Model
{
    protected $connection = 'todo';

    protected $table = 'tenants';

    protected $primaryKey = 'tenant_id';

    protected $fillable = ['tenant_uid', 'company_name', 'short_company_name', 'database_prefix', 'address'];
    .......



### Create the template, tenant migration folders as well as a tenant public folder ###
php artisan tenant:folders

This package registers a middleware called authTenant within it's service provider. This is needed in order to automatically set the default connections for the Application's model ### For a todo application [see demo application](https://github.com/gamerwalt/TodoMultitenantDemo) ### Inside your controllers that will control tenant specific routing, add the following to the __construct methods
$this->middleware('auth'); $this->middleware('authTenant'); //This is automatically pushed to laravel's kernel
also make sure to include the trait to do migrations automatically once the tenant has registered their company...
use MigrateTenantDatabase;
Complete namespace for this is...
use rndwiga\MultiTenant\Traits\MigrateTenantDatabase;
### Create your migration files ###
This assumes you have already done...
php artisan vendor:publish
So create your migration files...
php artisan make:migration create_todo_table --create=todos --path=database/migrations/tenant
#### Your TenantDatabase Model ###
**This is important**

Your TenantDatabase model **must** implement the ITenantDatabase interface
class TenantDatabase extends Model implements ITenantDatabase
Once that's done, start with registering a user, at least collect information needed. In your controller that registers the user, tenant, tenant_database, tenant_users you should be able to do...
$this->migrateTenantDatabase($tenantDatabase->hostname, $tenantDatabase->database_name, $tenantDatabase->user_name, $tenantDatabase->password);
Use [this](https://github.com/gamerwalt/TodoMultitenantDemo) demo as a guide.
See SessionsController@postRegister.
This should show the code...
$input = $request->all(); $email = $input['email']; $password = $input['password']; $name = $input['name']; $companyName = $input['company_name']; DB::beginTransaction(); $user = $this->registerUser($email, $password, $name); $tenant = $this->registerTenant($companyName); $tenantUser = $this->createTenantUserDetails($user, $tenant); $tenantDatabase = $this->createTenantDatabase($tenant); DB::commit(); //now we can migrate the database of the tenant using the tenant database settings $this->migrateTenantDatabase($tenantDatabase->hostname, $tenantDatabase->database_name, $tenantDatabase->user_name, $tenantDatabase->password);

Questions

Twitter: @rndwiga254, (*8)

Future

  1. Do some clean up of the code.
  2. Command for creating template and tenant migration files
  3. Command to migrate to template database
  4. Command to sync new migration files
  5. Command to copy any needed data from template database to all tenant databases
  6. Add tests

The Versions

02/02 2017

dev-master

9999999-dev

This package is used to create and manage laravel multitenant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

01/02 2017
29/02 2016

1.0.x-dev

1.0.9999999.9999999-dev

This package is used to create and manage laravel multitenant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Olawale Adewoyin

29/02 2016

1.1.x-dev

1.1.9999999.9999999-dev

This package is used to create and manage laravel multitenant applications

  Sources   Download

MIT

The Requires

 

The Development Requires

by Olawale Adewoyin