PHP code example of revoltify / tenantify

1. Go to this page and download the library: Download revoltify/tenantify 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/ */

    

revoltify / tenantify example snippets


'early' => env('TENANTIFY_EARLY', false),

'models' => [
    'tenant' => \App\Models\Tenant::class,
    'domain' => \App\Models\Domain::class,
],

'resolver' => [
    'class' => \Revoltify\Tenantify\Resolvers\DomainResolver::class,
    'cache' => [
        'enabled' => env('TENANTIFY_CACHE_ENABLED', false),
        'ttl' => env('TENANTIFY_CACHE_TTL', 3600),
    ],
],

use Revoltify\Tenantify\Models\Tenant;
use Revoltify\Tenantify\Models\Domain;

$tenant = Tenant::create([
    'name' => 'Example Organization'
]);

$tenant->domains()->create([
    'domain' => 'example.com'
]);

// Using facade
Tenantify::initialize($tenant);

// Using helper function
tenantify()->initialize($tenant);

// Initialize by ID
tenantify()->initialize($tenantId);

// Using helper functions
$tenant = tenant();
$tenantId = tenant_id();

// Using facade
$tenant = Tenantify::tenant();

use Revoltify\Tenantify\Bootstrappers\AbstractBootstrapper;
use Revoltify\Tenantify\Models\Contracts\TenantInterface;

class CustomBootstrapper extends AbstractBootstrapper
{
    protected int $priority = 10;

    public function bootstrap(TenantInterface $tenant): void
    {
        // Your bootstrapping logic here
    }

    public function revert(): void
    {
        // Your cleanup logic here
    }
}

'bootstrappers' => [
    \App\Bootstrappers\CustomBootstrapper::class,
],

use Illuminate\Database\Eloquent\Model;
use Revoltify\Tenantify\Models\Concerns\BelongsToTenant;

class Project extends Model
{
    use BelongsToTenant;

    protected $fillable = ['name', 'description'];
}

// Creates a project for the current tenant automatically
$project = Project::create([
    'name' => 'New Project'
]);

// Queries are automatically scoped to the current tenant
$projects = Project::all(); // Only returns current tenant's projects

// Access the tenant relationship
$tenant = $project->tenant;

use Illuminate\Contracts\Queue\ShouldQueue;
use Revoltify\Tenantify\Job\TenantAware;

class ProcessTenantData implements ShouldQueue, TenantAware
{
    public function handle(): void
    {
        // Job will automatically run in the correct tenant context
    }
}
bash
php artisan tenantify:install
bash
php artisan migrate