├── composer.json ├── src ├── MultiTenantServiceProvider.php ├── Middleware │ └── MultiTenant.php └── Console │ └── MigrateMultiTenant.php └── README.md /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mhnassar/multi-tenant", 3 | "description": "MultiTenant package / one application with multi databases schema", 4 | "keywords": [ 5 | "Multi", 6 | "databases", 7 | "schema", 8 | "MultiTenant", 9 | "Multi Database" 10 | ], 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Mahmoud Nassar", 15 | "email": "mahmoudnassar662@gmail.com", 16 | "role": "Developer" 17 | } 18 | ], 19 | "minimum-stability": "dev", 20 | "require": {}, 21 | "autoload": { 22 | "classmap": [ 23 | ], 24 | "psr-4": { 25 | "MHNassar\\MultiTenant\\": "src" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/MultiTenantServiceProvider.php: -------------------------------------------------------------------------------- 1 | commands('Console.MigrateMultiTenant'); 18 | } 19 | 20 | /** 21 | * Register the application services. 22 | * 23 | * @return void 24 | */ 25 | public function register() 26 | { 27 | // 28 | $this->registerCommands(); 29 | } 30 | 31 | protected function registerCommands() 32 | { 33 | $this->app->singleton('Console.MigrateMultiTenant', function ($app) { 34 | 35 | return new Console\MigrateMultiTenant(); 36 | }); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/Middleware/MultiTenant.php: -------------------------------------------------------------------------------- 1 | url(), PHP_URL_HOST)); 19 | $connectionsArray = config('database.connections'); 20 | 21 | if (count($url) > 1 && array_key_exists($url[0], $connectionsArray)) { 22 | $connection = $url[0]; 23 | session(['subDomain' => $connection]); 24 | 25 | } else { 26 | $connection = env('DB_CONNECTION'); 27 | } 28 | \Illuminate\Support\Facades\Config::set('database.default', $connection); 29 | 30 | return $next($request); 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/Console/MigrateMultiTenant.php: -------------------------------------------------------------------------------- 1 | argument('connection'); 42 | 43 | \Illuminate\Support\Facades\Config::set('database.default', $connection); 44 | $this->info('############# Database Convert Done ############'); 45 | Artisan::call('migrate', ['--seed' => true]); 46 | $this->info('############# Database Migrations Done ############'); 47 | \Illuminate\Support\Facades\Config::set('database.default', env('DB_CONNECTION')); 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # multiTenant 2 | Laravel package to handel multi databases in one sorce code , depend on sub domain . 3 | Suitable for marketing companies that like to re-use functionality for different clients or start-ups building the next software as a service. 4 | 5 | # Advantages 6 | - Very simple 7 | - Seperated Database Schema 8 | 9 | # Installation 10 | - Add in composer.json 11 | ```bash 12 | "mhnassar/multi-tenant": "dev-master" 13 | ``` 14 | - Run 15 | ```bash 16 | composer update 17 | ``` 18 | - Add in config/app.php 19 | ```bash 20 | 'providers' => [ 21 | ... 22 | MHNassar\MultiTenant\MultiTenantServiceProvider::class, 23 | ], 24 | ``` 25 | - let’s add middleware to the array of middleware in file Http/Kernel.php: 26 | ```bash 27 | protected $middleware = [ 28 | ...... 29 | MHNassar\MultiTenant\Middleware\MultiTenant::class, 30 | ]; 31 | 32 | ``` 33 | After going through all the previous steps, your package should be installed successfully. 34 | 35 | # Configuration Tenant Driver for Multiple Database 36 | 37 | - Create a new subdomain for your domain in your hosting server (e.g.: tenant1.example.com) 38 | - Create a new database with the same name of your subdomain (e.g. tenant1)- 39 | - Create a new database connection with the same name of your subdomain in the file config/database.php: 40 | 41 | ```bash 42 | 'tenant1' => [ 43 | 'driver' => 'mysql', 44 | 'host' => env('DB_HOST', 'localhost'), 45 | 'port' => env('DB_PORT', '3306'), 46 | 'database' => 'tenant1', //name of database 47 | 'username' => 'root', 48 | 'password' => 'root', 49 | 'charset' => 'utf8', 50 | 'collation' => 'utf8_unicode_ci', 51 | 'prefix' => '', 52 | 'strict' => true, 53 | 'engine' => null, 54 | ], 55 | 56 | ``` 57 | - change "APP_ENV" in .env file to "local" 58 | - To run migration and seeds run the "Multi:migrate" command with the name of your subdomain from the main folder: 59 | ```bash 60 | php artisan Multi:migrate tenant1 61 | ``` 62 | Now you are finished ... Enjoy! 63 | 64 | # ToDo List 65 | - wizard intallation 66 | - Dashboard to mange Tenates 67 | 68 | --------------------------------------------------------------------------------