Change which SQL Server schema Laravel Passport uses

Viewed 1764

A colleague (recently left) has installed Laravel Passport on a site I'm now working on, which uses SQL Server, with the various oauth_ tables created in the default dbo schema.

However, our database has a number of schemas, depending on the purpose of the tables, including 'security' which has all the tables relating to auth stuff.

I've been asked to make changes so the tables are in security not dbo, but I can't figure out how to achieve this. In the passport model files there's just a table name specified, and the migrations generated also seem to have no option for specifying a schema. I couldn't see anything in the Passport docs to indicate whether it's possible either.

Does anyone know if it's possible to have passport use tables in the non-default schema?

1 Answers

Well you can configure different connections with differents schemas under your database.php config file and then specify $connection in your model, example:

    'sqlsrv1' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
        'schema'   => 'schema_name1',
    ],


    'sqlsrv2' => [
        'driver' => 'sqlsrv',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
        'schema'   => 'schema_name2',
    ],

Then set the $connection property in desired model

 protected $connection = 'sqlsrv1';
Related