public static function bootMultiTenantTrait() { static::addGlobalScope(new MultiTenantScope()); static::saving(function ($model) { $organization = Organization::current(); $model->organization_id = $organization->id; }); }
/** * Remove the scope from the given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ public function remove(Builder $builder, Model $model) { $organization = Organization::current(); $query = $builder->getQuery(); foreach ((array) $query->wheres as $key => $where) { if ($where['type'] == $organization->id && $where['column'] == 'organization_id') { unset($query->wheres[$key]); $query->wheres = array_values($query->wheres); } } }
/** * @param string $message * @return User * @throws \App\Exceptions\ForbiddenException * @throws \App\Exceptions\MultiTenantException */ public function adminsOnly($message = 'Only admins may do this.') { $user = $this->mustBeLoggedIn(); if ($user->hasRole(Role::ROLE_SUPER_ADMIN)) { return $user; } $organization = Organization::current(); if ($user->hasRole($organization->roleAdminName())) { return $user; } throw new ForbiddenException($message); }