It is not supported
Eloquent does not currently support querying for relationship existence across databases. The relationships must exist within the same database.
https://laravel.com/docs/12.x/eloquent-relationships#querying-relationship-existence
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?
It is not supported
Eloquent does not currently support querying for relationship existence across databases. The relationships must exist within the same database.
https://laravel.com/docs/12.x/eloquent-relationships#querying-relationship-existence
Please or to participate in this conversation.