title: Laravel 5 Multi-Tenancy App with separate databases - users have access to multiple installations
body: <p>Over the past couple of years I have developed a very customised PHP/MySQL application that is used for a number of clients. I have been creating a new database and new installation for each client up to this point.</p>
  
  <p>The first obvious problem here is keeping multiple installations up to date with any code changes; a second problem is that each installation has a large amount of users; and for most clients; some of these users are the same - and they have to have a number of seperate user accounts and urls to remember.</p>
  
  <p>I am moving the application over to Laravel 5 at the moment and looking into the best implementation for multi-tenancy; so looking for a little advice on the best implementation. I've used Laravel before but am by no means an expert.</p>
  
  <p>This is what I am thinking as far as setup.</p>
  
  <p><strong>1 Master Database that holds tables for:</strong></p>
  
  <ol>
  <li>All User account information</li>
  <li>Access Control Table - which installations the users can access; what their user level on that installation is.</li>
  <li>Configuration table for each installation - database connection info, basic configuration etc.</li>
  </ol>
  
  <p><strong>Then a seperate database for each installation that contains all the information that is needed for, and submitted to, that installation.</strong></p>
  
  <p>The ideal setup is that a user can go to a subdomain i.e installationname.appname.com; sign in with their master login details and automatically go to the required installation; OR go to appname.com, sign in and then select which installation to connect to.</p>
  
  <p><strong>My questions are:</strong></p>
  
  <ol>
  <li>Is this the best arrangement to achieve what I am looking for.</li>
  <li>What's the best method for storing which installation the user is looking at (session variable)</li>
  <li>Can I define a model between 2 databases - perhaps define one connection as master connection, then dynamically define another connection using the database connection information in the master database to connect to the correct installation. - the system will often need to check the user info for access level etc.</li>
  </ol>
  
  <p>I'm sure there's a lot of issues that I have not thought of; but if anyone has any links or guidance that may help that would be great. First time asking a question on SO but have found a huge amount of research help here in the past so thanks to the community! </p>
  
  <hr>
  
  <p>UPDATE - So I think I have a way to make this work now; using seperate databases as above; set </p>
  
  <pre><code>protected $connection = 'tenant_connection'
  </code></pre>
  
  <p>in the models relating to tenant-specific database content.</p>
  
  <p>Then somewhere in a header file set the tenant_connection that is wanted based on a session variable which has been set on login/by subdomain.</p>
  
  <pre><code>$tenant = Installation::where('installation', '=', $session['installation'])-&gt;first();
  Config::set('database.connections.tenant_connection', array('driver' =&gt; 'mysql', 'host' =&gt; $tenant-&gt;db_hostname, 'username' =&gt; $tenant-&gt;db_username)... etc.
  </code></pre>
  
  <p>Assuming relationships will work across connections; I don't see why this would not work; just need to work out best place to set the tenant connection. </p>