Foreign key relationship to another database.

Submitted by orlissenberg - 9 years ago

Reference the connection by name, the database could be named differently in another environment.\ Note: you might want to add backticks to the database name if the name uses alternative characters.

use Illuminate\Database\Query\Expression;

$tableName = 'your_table';
$databaseName = DB::connection('another_database')->getDatabaseName();

$table->foreign('created_by', $tableName . '_created_by_foreign')
    ->references('id')->on(new Expression($databaseName . '.users'));