I am just using Laravel whereRelation
. It runs smooth and as expected if the relations between 2 tables is in the same database (DB_A
). The problem is when I have to use 2 databases (DB_A
and DB_B
). In the model relationship I put the connections and there is no problem
// Model user DB_A
protected $connection = 'mysql';
public function address()
{
return $this->hasOne(Address::class, 'id_user');
}
// Model address DB_B
protected $connection = 'mysql2';
protected $table = 'address';
public function user()
{
return $this->belongsTo(User::class, 'id_user');
}
In the query I want to get all the relationship between those 2 tables. The query like:
$data = User::with('address')->reorder('id', 'desc');
$data = $data->whereRelation('address', 'status', '=', 'verified');
I got the error say that table address
doesn't exist in DB_A
which is belong to DB_B
. If i remove the line whereRelation takeplace it doesn't give any errors. How to handle or specify the database name and table name in whereRelation
?
EDIT: Both DB_A
and DB_B
is in different Host.