title: Laravel order of middleware (Middleware Priority). Multi-tenant using Postgres
body: <p>In <strong>web.php</strong> I've switched Postgres schemas in middleware as the subdomain type of HTTP request is made. This way:</p>
  
  <pre><code>Route::group(
      [
          'domain'     =&gt; '{tenant}.' . config('app.url'),
          'middleware' =&gt; 'select-schema'
      ],
      function () {
          $this-&gt;get('/', 'HomeController@index')-&gt;middleware('auth');
      }
  );
  </code></pre>
  
  <p>In <strong>select-schema</strong> middleware, I do something like this. This works correctly. (don't worry)</p>
  
  <pre><code>DB::select('SET search_path TO ' . {tenant});
  </code></pre>
  
  <p>My main problem is that: I've different <code>migrations</code> for <code>public</code> schema and for any <code>individual tenant</code>. In <code>individual tenant</code> I have <code>users</code> table. As soon I'm logged in it pop up this error.</p>
  
  <blockquote>
    <p>SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "users" does not exist</p>
  </blockquote>
  
  <p><strong>The main issue is</strong></p>
  
  <pre><code>$this-&gt;get('/', 'HomeController@index')-&gt;middleware('auth');
  </code></pre>
  
  <p>The model works well but middleware <code>auth</code> execute first before  <code>select-schema</code></p>
  
  <p><strong>How do I order?</strong> <code>select-schema</code> then <code>auth</code></p>