Is it possible to reference a Foreign Key in a different host, different user, different database in laravel?

  amazon-rds, eloquent, laravel, mysql, php

my first database host in aws and secound database host in diedem server.
and I want to reference one column from laravel_1.uesrs table to multiple other database server in employees table .

Host: 192.168.0.100

Database:[laravel_1.uesrs]: https://i.stack.imgur.com/Tq5Mm.png

CreateUsersTable

Schema::create('users', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->string('name');
  $table->string('email');
}

Host: 10.0.0.12 (different server)

Database: [laravel_2.employees]: https://i.stack.imgur.com/QcLY5.png

CreateEmployeesTable

Schema::create('employees', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('uesr_id');
  $table->string('contact_number', 11)->nullable();
  $db = DB::connection('laravel_1')->getDatabaseName();
  $table->foreign('uesr_id')->references('id')->on(new Expression($db . '.uesrs'));
}
This code not working..

Source: Laravel

Leave a Reply