jeudi 26 juillet 2018

Laravel & Eloquent - Eager loading of Model which is on different database, returns undefined column

I have 2 databases. In one I have table 'users' in the other I have table e.g. 'agencies'

db1.users table
id | name | agency_id
---------------------
1  | name | 1

db2.agencies table
id | name 
----------
1  | name 

My model relation is specified like this

User
{
    //Uses default connection wich is db1 
    $connection = "db1"
}

Agency
{
    $connection = "db2";DB2

    public function user()
    {
        return $this->hasOne(User::class)
    }
}

When I am trying to eager loading agencies with their users, like this:

Agency::with(['user']);

Error is returned saying "Undefined column: 7 ERROR: column users.agency_id does not exist..."

The column agency_id obviously is there. Also I notice that it get the proper table "users".

If I move both table in one database, and spcefied equal connections, everything works perfectly. But when the two tables are in different databased, it returns SQL error that columno not found, despite I have it



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire