3

In web.php I've switched Postgres schemas in middleware as the subdomain type of HTTP request is made. This way:

Route::group(
    [
        'domain'     => '{tenant}.' . config('app.url'),
        'middleware' => 'select-schema'
    ],
    function () {
        $this->get('/', 'HomeController@index')->middleware('auth');
    }
);

In select-schema middleware, I do something like this. This works correctly. (don't worry)

DB::select('SET search_path TO ' . {tenant});

My main problem is that: I've different migrations for public schema and for any individual tenant. In individual tenant I have users table. As soon I'm logged in it pop up this error.

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "users" does not exist

The main issue is

$this->get('/', 'HomeController@index')->middleware('auth');

The model works well but middleware auth execute first before select-schema

How do I order? select-schema then auth

Sagar Chamling
  • 1,038
  • 1
  • 12
  • 26

2 Answers2

13

I've found the solution, For this, there's something called $middlewarePriority in App\Kernel.

Adding this help me solve the problem.

/**
 * Responsible for prioritizing the middleware
 *
 * @var array
 */
protected $middlewarePriority = [
    \App\Http\Middleware\SwitchSchema::class,
];

I've got solution from this link.

https://github.com/laravel/framework/issues/19565

Sagar Chamling
  • 1,038
  • 1
  • 12
  • 26
0

Have you tried wrapping your routes in the tenant group with another group? See if this works:

Route::group([
        'domain'     => '{tenant}.' . config('app.url'),
        'middleware' => 'select-schema'
    ],function () {
        Route::group(['middleware' => 'auth'], function () {
            Route::get('/', 'HomeController@index');
        });
    }
);