4 months ago

Cross database relationship with eloquent

Posted 4 months ago by Crazylife

How to set up the database connection to allow cross database with eloquent method?

For example, I have table A and table B.

My table A model

 public function branch()
        return $this->belongsTo('App\Models\tableA', 'id');

I wrote a query

$query = tableA::on('second_db')->get();

// looping result in blade

@foreach($query as $q)
{{ $q->branch->name }}

I found that the branch doesn't has any connection with it. How can i make this work?

Relationship doesnt use connection of parent model.

Please sign in or create an account to participate in this conversation.