Cover image for Orchid Platform meets multi-tenancy

Orchid Platform meets multi-tenancy

ostap profile image Ostap Bregin ・2 min read

Orchid is a Platform for back-office applications on Laravel. With it, you can create beautiful apps faster, using only PHP code.

Tenancy is a feature, allows you to split data each user use (The way how SaaS works).

Here are a few steps you can use to integrate multi-tenancy for your orchid application.

1. Install tenancy package

For tenancy package, I will use stancl/tenancy.

Go to installation page and run 2 simple commands as written :)

composer require stancl/tenancy
php artisan tenancy:install

2. Configure domain detection

2.1. Go to the config/tenancy.php and specify all your domains (local, production) in central_domains key.


    'central_domains' => [

2.2. Move all your platform routes from routes/platform.php to routes/tenant.php

Note: if you want to add acess control, you can do it here by specifying middleware:

Route::middleware('auth', 'tenant.access')->group(function (){
    // Your content here

Sample access-middleware:

class CheckTenantAccess
    public function handle(Request $request, Closure $next)
        if (!auth()->user()->tenants->contains(tenant('id'))){
            return redirect()->route('workspaces.index');
        return $next($request);

2.3 Add macro to get subdomain

Inside boot method in AppServiceProvider, add these lines:

use Illuminate\Http\Request;
Request::macro('subdomain', function () {
    return current(explode('.', $this->getHost()));

2.4. Automatically add subdomain as a parameter to every platform route:

Create new middleware with content:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

class TenancyDomain
    public function handle(Request $request, Closure $next)
        // Add current subdomain as a parameter to all route links
        URL::defaults(['subdomain' => $request->subdomain()]);

        // Fix bug - accidentally the subdomain is put as the first parameter in the package controller

        return $next($request);

And register it in app/Http/Kernel.php:

class Kernel extends HttpKernel
    protected $middlewareGroups = [
        'web' => [

2.5 Go to config/platform.php, and update 'domain' line:

    'domain' => env('DASHBOARD_DOMAIN', '{subdomain}.yourdomain.test'),
    'prefix' => env('DASHBOARD_PREFIX', '/'),

3. Move migrations

Move your migrations to a 'database/migrations/tenant' directory.

That's it! :)

Posted on by:

ostap profile

Ostap Bregin


I build stuff with Laravel. 15 y/o.


markdown guide