1

I've been following the guide here and have it all setup. The guide explains how to grab the subdomain and use this to connect to the correct database. Each tenant will have its own db and there will also be a master _admin db. This _admin db will have a tenants table with a subdomain column. There is a filter that is run each time that checks the subdomain against the tenants table. The problem comes as the database config file isnt set to the master _admin db which includes the tenants table but to mysql_tenant, where tenant is set on the fly.

I think I can get around this by manually specifying the db for the filter to connect to, here is the code I have for the filter.

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'));
});

The line i need to alter is:

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

I've tried doing the following but get an error:

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

The error i get is:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

1 Answer 1

1

Use the on() method to specify the connection:

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

Substitute the name of your database connection for db_connection (mysql in your example).

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant, something so simple and i've been trying to figure it out for a couple of hours!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.