I saw in several places to โstay awayโ from this, but, alas, this is how my database is built:
class Album extends Eloquent {
and table of genres:
class Genre extends Eloquent { protected $connection = 'Resources'; }
My database.php:
'Resources' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'resources', 'username' => 'user', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'my_data', 'username' => 'user', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
and when I try to run
Album::whereHas('genre', function ($q) { $q->where('genre', 'German HopScotch'); });
it is not selected properly (does not add the database name to the "genres" table):
Next exception 'Illuminate\Database\QueryException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_data.genres' doesn't exist
It is important to note that this works great:
Album::first()->genre;
Update
The best I've found so far is to use the from method to create a specific connection. I found that the builder inside the request can get "from"
Album::whereHas('genre', function ($q) { $q->from('resources.genres')->where('genre', 'German HopScotch'); });
This is a decent solution, but it requires me to dig the php database and find a good way to get the correct table and database name from the relationship genre.
I would appreciate it if someone else could use this solution and make it more general.