Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm just taking a stab at this, so be warned :) The DatabaseManager class, which is used whenever you call DB, has and extend method. Here's the <a href="https://github.com/laravel/framework/blob/master/src/Illuminate/Database/DatabaseManager.php#L160">link to the source</a>. The DB::connection() method should return an instance of <a href="https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connection.php#L95">Illuminate\Database\Connection</a>. From all this, I would create a new user connection as follows:</p> <pre><code>$user = Auth::user(); DB::extend($user-&gt;username, function() use ($user) { // $pdo = new PDO(); set this up how you see fit return new Illuminate\Database\Connection($pdo, $user-&gt;databaseName, $tablePrefix); }); </code></pre> <p>Personally, I would add a new method to each user, User::databaseConnection(), and call that when I extend the DatabaseManager.</p> <pre><code>DB::extend($user-&gt;username, function() use ($user) { return $user-&gt;databaseConnection(); }); </code></pre> <p>Through out your application you should be able to call a registered user's connection via:</p> <pre><code>DB::connection(Auth::user()-&gt;username); </code></pre> <p><strong>Update</strong></p> <p>Depending on how often and when you'd be calling the tenant connection, you may want to use the IOC container.</p> <pre><code>App::bind('tenantDB', function() { return DB::connection(Auth::user()-&gt;username); }); App::make('tenantDB')-&gt;insert(...); </code></pre> <p>I forgot about migrations and seeding. For migrations, you can set the file path</p> <pre><code>php artisan migrate:make foo --path=app/migrations </code></pre> <p>So if you use the Config class to set the default database or DB::setDefaultConnection($username), I'd assume all migrations and seeding will be done for the current connection. When that process if complete you can switch back to you main database.</p> <p><strong>Update 2</strong></p> <p>The laravel developers are amazing and I should have definitely got the urge to check this out sooner than later. You can do migrations and seed on any database connection you've created. </p> <pre><code>artisan migrate --database='userConnectionName' artisan db:seed --database='userConnectionName' </code></pre> <p>Looking at Barry's answer, that is probably a good deal simpler than extending the DatabaseManager.</p> <p>If you want to see all the options for these commands just run:</p> <pre><code>artisan help migrate artisan help db:seed </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload