In the future, if you ever change the database name, this will not work. It is best to get the database name by database definition.
config / database.php
'auth_database' => [ 'driver' => 'mysql', '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' => false, 'engine' => null, ],
Then in the migration file:
Schema::create('role_user', function (Blueprint $table) { $db = DB::connection('auth_database')->getDatabaseName(); $table->integer('role_id')->unsigned(); $table->foreign('role_id')->references('id')->on('roles'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on(new Expression($db . '.users')); });
source share