1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 18:04:03 +00:00
tenancy/src/Database/TenantScope.php
Samuel Stancl c32f52ce7c
phpstan fix: Scope generics
phpstan started failing with '... implements generic interface
Illuminate\Database\Eloquent\Scope but does not specify its types:
TModel'. We solve this by adding an implements docblock to the scopes
implementing that interface. They're fairly generic - we just use the
Model type itself in the code - so we use Model for the type parameter.
2026-04-15 11:23:12 +02:00

36 lines
854 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;
/** @implements Scope<Model> */
class TenantScope implements Scope
{
/**
* @param Builder<Model> $builder
*/
public function apply(Builder $builder, Model $model)
{
if (! tenancy()->initialized) {
return;
}
$builder->where($model->qualifyColumn(Tenancy::tenantKeyColumn()), tenant()->getTenantKey());
}
/**
* @param Builder<Model> $builder
*/
public function extend(Builder $builder): void
{
$builder->macro('withoutTenancy', function (Builder $builder) {
return $builder->withoutGlobalScope(static::class);
});
}
}