hipsterjazzbo / Landlord

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

Added a hook to modify the query builder for special use cases #70

Closed OwenMelbz closed 2 years ago

OwenMelbz commented 7 years ago

Adds the ability to call applyExtraTenantScopes which allows you to modify the query builder.

This is extremely useful for when you need to apply extra logic, for example

where tenant_id = 1 or user_type == 'super_admin'


We found ourself in a situation where our client required a "super user" mode which gave him a separate subdomain to access the application through, and engage with all the tenants.

This posed an issue as when you then view the super users engagement from one of the tentants, as the super user did not exist within their scope, their interactions would never display.

By allowing access to the builder we can modify it when needed for special situations. for example

class User {

    use BelongsToTenants;

    public function applyExtraTenantScopes($builder, $tenant, $id)
    {
        $builder->orWhere('users.is_super_user', 1);
    }
}
OwenMelbz commented 7 years ago

🙄