PHP code example of angelitosystems / filament-tenancy
1. Go to this page and download the library: Download angelitosystems/filament-tenancy 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/ */
angelitosystems / filament-tenancy example snippets
// How tenants are resolved from requests
'resolver' => env('TENANCY_RESOLVER', 'domain'), // 'domain', 'subdomain', 'path'
// Central domains that won't be resolved as tenants
'central_domains' => [
'app.dental.test',
'localhost',
env('APP_DOMAIN', 'localhost'),
],
use AngelitoSystems\FilamentTenancy\Facades\Tenancy;
// Create a new tenant
$tenant = Tenancy::createTenant([
'name' => 'Acme Corporation',
'slug' => 'acme-corp',
'domain' => 'acme.com',
'is_active' => true,
]);
// Switch to tenant context
Tenancy::switchToTenant($tenant);
// Run code in tenant context
Tenancy::runForTenant($tenant, function () {
// This code runs in the tenant's database context
User::create(['name' => 'John Doe', 'email' => '[email protected]']);
});
// Switch back to central context
Tenancy::switchToCentral();
namespace App\Models;
use AngelitoSystems\FilamentTenancy\Concerns\HasRoles;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasRoles;
// Your model code...
}
use AngelitoSystems\FilamentTenancy\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use BelongsToTenant;
// Your model code...
}
use AngelitoSystems\FilamentTenancy\Concerns\UsesLandlordConnection;
use Illuminate\Database\Eloquent\Model;
class Plan extends Model
{
use UsesLandlordConnection;
// Your model code...
}
// app/Providers/Filament/AdminPanelProvider.php
use AngelitoSystems\FilamentTenancy\FilamentPlugins\TenancyLandlordPlugin;
public function panel(Panel $panel): Panel
{
return $panel
->id('admin')
->path('/admin')
->plugin(TenancyLandlordPlugin::make())
// ... other panel configuration
}
// app/Providers/Filament/TenantPanelProvider.php
use AngelitoSystems\FilamentTenancy\FilamentPlugins\TenancyTenantPlugin;
public function panel(Panel $panel): Panel
{
return $panel
->id('tenant')
->path('/admin')
->plugin(TenancyTenantPlugin::make())
// ... other panel configuration
}
use AngelitoSystems\FilamentTenancy\Events\TenantCreated;
Event::listen(TenantCreated::class, function (TenantCreated $event) {
// Handle tenant creation
$tenant = $event->tenant;
});
use AngelitoSystems\FilamentTenancy\Support\TenantResolver;
class CustomTenantResolver extends TenantResolver
{
public function resolve(Request $request): ?Tenant
{
// Your custom resolution logic
return parent::resolve($request);
}
}
// Register in a service provider
$this->app->bind(TenantResolver::class, CustomTenantResolver::class);