Laravel很多很多数据透视表不同的数据库

Laravel 5.5

I've got two models, User and Conversation

User to Conversations is a many to many relationship (both ways).

My table structure is as follows:

conversation is on database_1
conversation_user is on database_1
user is on database_2

Inside App\Conversation.php:

protected $connection = 'database_1';
protected $table = 'conversations';
public function users()
{
    return $this->belongsToMany("App\User");
}

Inside App\User.php:

protected $connection = 'database_2';
protected $table = 'users';
public function conversations()
{
    return $this->belongsToMany("App\Conversation");
}

All of this is on the same server but is there a way to have this working or not ?

When querying the relationship on Conversation to get users, it's looking for database_2.conversation_user instead of database_1.conversation_user

So in essence, I need to say that the Pivot table is located in database_1, is there a way to do this?

One way to work around this is using the query builder to create the relationship. In your User model try:

public function conversations()
{
    return DB::connection('db1_connection_name')->table('conversation_user')->where('user_id', $this->id)->get();
}

Apparently you can prefix the connection in a relationship also:

public function conversations() {
    return $this->belongsToMany(User::class, env('DB_CONNECTION_1').'.conversation_user', 'user_id', 'conversation_id');
 }