Skip to content

alshakibeelahi/tenancy-test

Repository files navigation

🏗️ Laravel Tenancy Setup (stancl/tenancy 3.x)

✅ Step 1: Install Laravel & Package

composer create-project laravel/laravel your-project-name
cd your-project-name
composer require stancl/tenancy

✅ Step 2: Install Tenancy Setup Files

php artisan tenancy:install
php artisan migrate

This creates:

  • config/tenancy.php
  • routes/tenant.php
  • tenant migrations
  • provider: App\Providers\TenancyServiceProvider

✅ Step 3: Register Tenancy Service Provider

Open bootstrap/providers.php and add:

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\TenancyServiceProvider::class, // ✅ Add this
];

✅ Step 4: Create Custom Tenant Model

Create: app/Models/Tenant.php

namespace App\Models;

use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;

class Tenant extends BaseTenant implements TenantWithDatabase
{
    use HasDatabase, HasDomains;
}

Then update your config:

File: config/tenancy.php

'tenant_model' => \App\Models\Tenant::class,

✅ Step 5: Central Domains

Edit config/tenancy.php:

'central_domains' => [
    'localhost', // or '127.0.0.1'
],

✅ Step 6: Central Route Restriction (in api.php or web.php)

foreach (config('tenancy.central_domains') as $domain) {
    Route::domain($domain)->group(function () {
        // Central routes here
        Route::post('/create-organization', [TenantController::class, 'store']);
    });
}

✅ Step 7: Tenant Routes

In routes/tenant.php:

use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use App\Models\User;

Route::get('/', function () {
    dd(User::all());
});

✅ Only available on tenant domains, e.g. acme.localhost


✅ Step 8: Move User Migration to Tenant Folder

mv database/migrations/xxxx_create_users_table.php database/migrations/tenant/

Now, each tenant will have its own users table when created.


✅ Tenant Creation via Tinker

php artisan tinker
>>> $tenant1 = App\Models\Tenant::create(['id' => 'acme']);
>>> $tenant1->domains()->create(['domain' => 'acme.localhost']);

Then seed a user:

>>> $tenant1->run(fn () => \App\Models\User::factory()->create());

✅ Try Accessing

Visit:

  • http://localhost → central app
  • http://acme.localhost → tenant app (you’ll see dumped users)

🔁 Later: Your Own Flow

You extended it to support:

✅ Organization API: Central Route

POST /create-organization

  • Creates tenant
  • Creates domain
  • Adds employee to tenant DB

You used:

Tenant::create([...])->run(function () {
    Employee::create([...]);
});

✅ Tenant Routes: /employees

You made:

// routes/tenant.php
Route::get('/employees', function () {
    return Employee::all();
});

Optional:

Route::prefix('api')->group(function () {
    Route::get('/employees', ...);
});

✅ Localhost Subdomain Testing

In hosts file:

127.0.0.1 acme.localhost

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages