1. Go to this page and download the library: Download 16bit/easy-multitenancy 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/ */
use Bit16\EasyMultitenancy\Facades\Tenant;
// Get current tenant
$currentTenant = Tenant::current(); // Returns tenant identifier (e.g., 'acme')
// Get current tenant ID (alias for current())
$tenantId = Tenant::id();
// Get current database path
$database = Tenant::database();
// Check if tenant exists
if (Tenant::exists('acme')) {
// Tenant exists
}
// Get all tenants
$tenants = Tenant::all();
// Manually switch tenant (rarely needed)
Tenant::identify('acme');
// Forget current tenant context
Tenant::forget();
use Bit16\EasyMultitenancy\Events\TenantIdentified;
use Bit16\EasyMultitenancy\Events\TenantNotFound;
use Bit16\EasyMultitenancy\Events\DatabaseSwitched;
// Listen to tenant identified event
Event::listen(TenantIdentified::class, function ($event) {
// $event->tenant
// $event->database
});
// Listen to database switched event
Event::listen(DatabaseSwitched::class, function ($event) {
// $event->tenant
// $event->database
// $event->connection
});
// Listen to tenant not found event
Event::listen(TenantNotFound::class, function ($event) {
// $event->tenant
});
use Bit16\EasyMultitenancy\Traits\UsesCentralConnection;
use Illuminate\Database\Eloquent\Model;
class Organization extends Model
{
use UsesCentralConnection;
}
use Bit16\EasyMultitenancy\Facades\Tenant;
use Illuminate\Support\Facades\Route;
Tenant::centralRoutes(function () {
Route::get('/', [LandingController::class, 'index']);
Route::get('/pricing', [PricingController::class, 'index']);
});
use Bit16\EasyMultitenancy\Contracts\GlobalJob;
class BackupAllTenants implements ShouldQueue, GlobalJob
{
// Runs in the central context, without a tenant.
}