Closed tklie closed 3 years ago
I'll look into it.
A possible suggested solution to this was to also define the schema's name in the table parameter like $table = 'dev_shop_db.categories. Unfortunately this is not possible here, since the schema's name changes depending on the .env file - and expressions are not allowed as field default values.
Have you tried overriding the model's getTable()
method? This allows you to use expressions.
You are correct. Overriding the getTable()
method like so does solve the SQL error (the relationship is still returning null
, but that's probably on me somehow).
public function getTable()
{
return env('SHOP_DATABASE', $this->connection) . '.' . $this->table;
}
Still feels like the framework should do this anyway. Otherwise, defining the $connection
property seems only 50% useful. But I'm guessing that's then more of an Eloquent issue rather than this package's?
Yeah, this is native Laravel behavior.
The whole topic is quite complex and I don't think Laravel is going to handle this case anytime soon.
Alright, thanks for looking into it though.
BelongToThrough does not respect Model's connection
Database structure
Let's assume a database structure with three models in two different database schemas.
Database config
And the following
config/database.php
which let's us switch between a development and produciton database using the.env
file:Relationships
The relationships are defined as follows:
Issue
When trying to retrieve an
Item
with it'sSeller
:The following SQL Exception is thrown:
As we can see, the query tries to find
dev_user_db.categories
even though$connection = 'shop_db'
was defined on theCategory
model.A possible suggested solution to this was to also define the schema's name in the
table
parameter like$table = 'dev_shop_db.categories
. Unfortunately this is not possible here, since the schema's name changes depending on the.env
file - and expressions are not allowed as field default values.So it would be great if the
belongsToThrough
relationship used the defined$connection
attributes to determine the correct database schemas to query.