public applyTenantScopes ( |
||
$model |
public function applyTenantScopes(Model $model)
{
if (!$this->enabled) {
return;
}
$this->modelTenants($model)->each(function ($id, $tenant) use($model) {
$model->addGlobalScope($tenant, function (Builder $builder) use($tenant, $id, $model) {
$builder->where($model->getTable() . '.' . $tenant, '=', $id);
});
});
}
/** * Boot the trait. Will apply any scopes currently set, and * register a listener for when new models are created. */ public static function bootBelongsToTenants() { // Grab our singleton from the container static::$landlord = app(TenantManager::class); // Add a global scope for each tenant this model should be scoped by. static::$landlord->applyTenantScopes(new static()); // Add tenantColumns automatically when creating models static::creating(function (Model $model) { static::$landlord->newModel($model); }); }