Framework: whereHas not working when one model is from a different database

Created on 10 Jun 2014  路  6Comments  路  Source: laravel/framework

I'm using Laravel 4.1.
Say cars can be co-owned by many owners. Each owner can own many cars.
If one of the tables is in a totally different database, the whereHas statement doesn't seem to work.

Say a model has a method cars():
MODEL: OWNER
public function cars()
{
return $this->belongsToMany('Car', DB_ONE.'.'.TABLE_PIVOT_CAR_OWNER);
}

MODEL: CAR
protected $connection = 'cars'; //Stored in a different database
public function owners()
{
return $this->belongsToMany('Owner', DB_ONE.'.'.TABLE_PIVOT_CAR_OWNER);
}

_The error say it is trying to find the table in the default database but it's not stored in the default database even though the models are configured correctly._
Example from documentation (assume content exists):
$o = Owner::whereHas('cars', function($q)
{
$q->where('content', 'like', 'foo%');

})->get();

Most helpful comment

I added package to manage this issue:

https://github.com/hoyvoy/laravel-cross-database-subqueries

All 6 comments

Running queries across connections like that is not supported.

Is this supported now ?

I added package to manage this issue:

https://github.com/hoyvoy/laravel-cross-database-subqueries

Gracias totales @maguilar92 funciona!

Is there a way to do this without an external package?

https://github.com/laravel/framework/issues/24398#issuecomment-511891818
Its works fine for me but both databases should be in the same server

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

YannPl picture YannPl  路  3Comments

JamborJan picture JamborJan  路  3Comments