Orchid Platform meets multi-tenancy
Tags: tenancy, orchid, orchidplatform, laravel
Published: 2020-07-20 21:27:02

---
title: Orchid Platform meets multi-tenancy
published: true
description: A few steps you can use to integrate multi-tenancy Orchid-based application.
tags: tenancy, orchid, orchidplatform, laravel
cover_image: https://raw.githubusercontent.com/orchidsoftware/platform/master/.github/IMAGES/promo-full.png
---

[Orchid](orchid.software) 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](orchid.software) application.

## 1. Install tenancy package

For tenancy package, I will use `stancl/tenancy`.

Go to [installation page](https://tenancyforlaravel.com/docs/v3/installation) and run 2 simple commands as written :)

```bash
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`](https://github.com/stancl/tenancy/blob/3.x/assets/config.php#L19) key.

Example:

```php
    'central_domains' => [
        '127.0.0.1',
        'localhost',
        'businessmachine.wip',
        'businessmachine.local',
        'businessmachine.app',
    ],
```

**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:


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

Sample access-middleware:

```php
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:

```php
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:

```php
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()]);

        // Do not inject 'subdomain' parameter in controllers
        $request->route()->forgetParameter('subdomain');

        return $next($request);
    }
}
```

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

```php
class Kernel extends HttpKernel
...
    protected $middlewareGroups = [
        'web' => [
            ...
            \App\Http\Middleware\TenancyDomain::class,
        ],
...
```

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

```php
    '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! :)