Laravel multi db tenancy with login using main users table

597 views Asked by At

I want to do multi db multi tenancy for a saas type shop app so that users is in main db, and users login using this table, and all shops have different db and users_table have shops db id, and all the shops regarded model uses tenant connection.

I tried using Stancl/Tenancy but its all about domains and subdomains, i want it to be done using single domain and use users_table to store tenant_id and tenant_db_name and initialize tenant from there. If its doable without using tenancy package also its fine. I am new to laravel so an example will be kindly appreciated.

1

There are 1 answers

0
Marsad Akbar On

This answer is late, but it will help future noobs like me.

I am building a SAAS application using Laravel, where my requirement is to use a single domain for the central and tenant apps, and each tenant should have it's own database. I came across the stancl/tenancy Laravel package. The problem is that some people, like me, don't understand the documentation due to a lack of experience and language. So here I would try to make it a simple step-by-step guide.

Step 1: install fresh Laravel

composer create-project laravel/laravel example-app

Step 2: install laravelfortenancy package

composer require stancl/tenancy

Run the Required Necessary commands

php artisan tenancy:install

Then add the service provider to your config/app.php file:

App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\TenancyServiceProvider::class, // <-- here

Now, as my requirement was to use a single domain with multi-database tenancy,. The default behaviour of the package is to use the web.php file for central routes and tenant.php for tenant routes. For that, I discarded the tenant.php file and used web.php for both the tenant and central applications.

make customer middleware using command

php artisan make:middleware VerifyTenant

In verifyTenant, paste this code

public function handle(Request $request, Closure $next): Response
{
    if(Auth::check()){
        $user = \auth()->user();
        $tenant = $user->tenant;
        if($tenant){
            Tenancy::initialize($tenant);
        }
    }else{
        return Redirect::route("login");
    }

    return $next($request);
}

In web.php all the routes belongs to central just on bottom of web.php paste this code. This is specific middleware you can put all tenants route.

Route::middleware(["verifyTenant"])->group(function () {
    
});

This way when you hit any route maybe it central or tenant it filtered and secured by middleware.

The Answer is sole purpose of learning and maybe not the best possible solution. This is my personal experience maybe I forgot to mention something or have done something wrong but I'm currently building SAAS and so far I don't see any problem with these configuration and setup. Any suggestion or improvements are welome. Sorry for my bad English :)