Back

Laravel 5.7: User-tables in other database


Hi,

In my Laravel project, my users-table and password_resets-table are located in the database Internal. Everything else is located in a dedicated database (default connection). I've setup my connections in my config/database.php.

'mysql' => [
	...
	'host' => env('DB_HOST', '127.0.0.1'),
	'port' => env('DB_PORT', '3306'),
	'database' => env('DB_DATABASE', 'forge'),
	'username' => env('DB_USERNAME', 'forge'),
	'password' => env('DB_PASSWORD', ''),
	...
],

'mysql_auth' => [
	...
	'host' => env('DB_HOST', '127.0.0.1'),
	'port' => env('DB_PORT', '3306'),
	'database' => env('DB_AUTH', 'forge'),
	'username' => env('DB_USERNAME', 'forge'),
	'password' => env('DB_PASSWORD', ''),
	...
],

I've configured my User-model to lookup its data in the Internal database

class User extends Authenticatable
{
	...
	
	protected $connection = 'myskkkql_auth';
	protected $table = 'Internal.users';

	...
}

But somehow this doesn't seem to be working in Laravel 5.7. It worked perfectly in Laravel 5.4. Did something change around the multi-database setup in Laravel? I've looked through the docs but can't find anything...

JerryHayes replied 1 week ago Solution

Not sure about Laravel 5.7. No idea, really why it does not work for you at Laravel 5.7. From my side it works.

laravel essay dune

Pieterjan replied 1 week ago

I'm sorry.

I noticed I've forgotten to adopt the same changes to my other Laravel project. I have 1 project that's only used to create the Internal database (users and password_resets). 2 other Laravel projects where the User-model needs to fetch the data from the Internal database.

I've setup the database link in the User-model in the first project, but apparently I've forgotten to do so in the second project. Now everything works as expected.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us