Support the ongoing development of Laravel.io →
Authentication Security Database
Last updated 3 weeks ago.
0

I've solved the above issue but ran into another with that same thread.

It says to make the default database mysql_tenant, but when people access the site with a subdomain such as 123.app.com it will try and look in the tenants table for the subdomain field of 123. Of course the default database is set to mysql_tenant and not the _admin one which contains the tenant table.

So when the following is executed it complains that the 123.tenants table doesn't exist which is correct.

$tenant = Tenant::where('subdomain', '=', $subdomain)->first();

Now this bit of code will only ever need to access the tenants table within the _admin database instance so i should be able to manually tell it which database connection to use. I've tried using something like;

$tenant = DB::connection('mysql')->Tenant::where('subdomain', '=', $subdomain)->first();

But this doesnt work, any tips on the right syntax?

For completness, this is the filter im using.

Route::filter('verifyTenant', function($route, $request) 
{
    $host = $request->getHost();
    $parts = explode('.', $host);
    $subdomain = $parts[0];

    # Ping DB for tenant match. Note that my Tenant model directs laravel to ping the tenant table in the master db to verify tenant
    $tenant = Tenant::where('subdomain', '=', $subdomain)->first();

    # If tenant database exists but tenant not in master db, redirect to homepage
    if ($tenant == null) return Redirect::to('http://www.'.Config::get('app.domain'));
});
Last updated 3 weeks ago.
0

Sign in to participate in this thread!

LoadForge

Your banner here too?

nabberuk nabberuk nabberuk Joined 18 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.