1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-15 02:24:04 +00:00
tenancy/src/Database/TenantScope.php
2022-11-10 16:44:52 +01:00

29 lines
706 B
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Stancl\Tenancy\Tenancy;
class TenantScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
if (! tenancy()->initialized) {
return;
}
$builder->where($model->qualifyColumn(Tenancy::tenantKeyColumn()), tenant()->getTenantKey());
}
public function extend(Builder $builder): void
{
$builder->macro('withoutTenancy', function (Builder $builder) {
return $builder->withoutGlobalScope($this);
});
}
}