1. Go to this page and download the library: Download danthedj/multitenant library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
'tenant_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => '', // The database name will be filled in dynamically upon the tenant being resolved.
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'database_prefix' => '', // this can be changed and represents a database prefix e.g. 'business_acme'.
'strict' => true,
'engine' => null,
],
$tenant = new \DanTheDJ\MultiTenant\Tenant();
// The unique name field identifies the tenant profile
$tenant->name = 'ACME Inc.';
// The non-unique email field lets you email tenants
$tenant->email = '[email protected]';
// The unique subdomain field represents the subdomain portion of a domain and the database table prefix
// Set subdomain and alias_domain field to NULL to access tenant by ID instead
$tenant->subdomain = 'acme';
// The unique alias_domain field represents an alternate full domain that can be used to access the tenant
// Set subdomain and alias_domain field to NULL to access tenant by ID instead
$tenant->alias_domain = 'acmeinc.com';
// The non-unique connection field stores the Laravel database connection name
$tenant->connection = 'db1';
// The meta field is cast to an array and allows you to store any extra values you might need to know
$tenant->meta = ['phone' => '123-123-1234'];
$tenant->save();
// get the resolver instance
$resolver = app('tenant');
// check if valid tenant
$resolver->isResolved();
// get the active tenant (returns Tenant model or null)
$tenant = $resolver->getActiveTenant();
// get all tenants (returns collection of Tenant models or null)
$tenants = $resolver->getAllTenants();
// activate and run all tenants through a callback function
$resolver->mapAllTenants(function ($tenant) {});
// reconnect default connection enabling access to "tenants" table
$resolver->reconnectDefaultConnection();
// reconnect tenant connection disabling access to "tenants" table
$resolver->reconnectTenantConnection();
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Tenant;
class CustomTenantServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton('TenantContract', function()
{
return new Tenant();
});
}
public function register()
{
//
}
}