spatie / laravel-multitenancy

Make your Laravel app usable by multiple tenants
https://spatie.be/docs/laravel-multitenancy
MIT License
1.08k stars 154 forks source link

Issue with the has function on different connections #525

Closed PittacusW closed 5 months ago

PittacusW commented 6 months ago

Hello,

I have two models, one on landlord connection, one in the tenant connection. These models are related with the hasMany relationship.

On the parent model, i have this function:

public function childs() {
    return $this->hasMany(Child::class);
  }

Now, on the controller, i call this:

public function list() {
    $records = Parent::has('childs')
                            ->orderBy('code')
                            ->get();

    return response()->json(compact('records'));
  }

But the query throws an error because it tries to find "childs" on the same connection as "parent". Is there a workaround this?

masterix21 commented 6 months ago

Relations between landlords and tenants aren't available. You can retrieve children only when your tenant is already configured as current, so:

Parent::first()->execute(function (Parent $parent) {
      $children = $parent->children()->orderBy('code')->get();
});