SPresnac's avatar

whereRelation using another connection

I do have a relation in one of my models, that uses a different db-connection.

So, some code: Model Employee:

class Employee extends Model
{
    protected $connection = 'kpidashboard';
    protected $table = 'employees';

    protected $fillable = [
        'employee_name',
        'kuerzel',
        'username',
        'active_employee',
    ];

    public function user(): HasOne
    {
        return $this->hasOne(
            related: User::class,
            foreignKey: 'username',
            localKey: 'kuerzel',
        );
    }

}

I do use this in another model, that uses the default mariadb connection:

class SageAufFSchrift extends Model
{
 // lots of other code
    public function responsible(): HasOne
    {
        return $this->hasOne(
            related: Employee::class,
            foreignKey: 'kuerzel',
            localKey: 'free_c03',
        );
    }

What I want to do now is: SageAufFSchrift::query()->whereRelation('responsible', 'username', 'someusername');

But it ends with

Base table or view not found:   1146 Table 'crm.employees' doesn't exist  ....

Where crm is the mariadb-schema for my connection.

Is Eloquent just missing to use the connection or am I doing something wrong here?

0 likes
3 replies
SPresnac's avatar

Meh, thanks for the hint. So, one needs to build a way around this ...

krisi_gjika's avatar

is this secondary db connection on the same sql server? or is it a completely external system. If it's external I think you have to treat it as such in your application and not mix it with your internal queries.

Please or to participate in this conversation.