Foreign key relationship to another database.

Submitted by orlissenberg - 3 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'));

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks