hipsterjazzbo / Landlord

A simple, single database multi-tenancy solution for Laravel 5.2+
MIT License
612 stars 142 forks source link

Call allTenants() on a relationship #95

Open warksit opened 6 years ago

warksit commented 6 years ago

I would like to setup a relationship that returns allTenants() for a relationship. Is there a way to do that?

public function holidays() {
    // Correctly returns tenant scoped Holidays
    return $this->hasMany('Holidays');
}

public function allHolidays() {
    // Want all holidays not scoped
    return $this->hasMany('Holidays')->allTenants();
    // Fails as allTenants() needs to be called statically
}

Also tried

public function allHolidays() {
    // Want all holidays not scoped
    Holidays::allTenants();
    return $this->hasMany('Holidays');
    // No error but holidays are still scoped
}

I don't want to Tenant::disable() as I do still want everything else scoped.

jbrooksuk commented 6 years ago

You can do:

public function allHolidays()
{
    return $this->hasMany('Holidays')->withoutGlobalScope('tenant_id');
}