1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 20:34:04 +00:00

Fix chaining withoutPending() with where() (#1457)

At the moment, `where()` cannot be used correctly while using
`withoutPending()`. For example, if we have a single non-pending tenant
in our DB (with ID 'foo'), queries like
`Tenant::withoutPending()->where('id', 'nonexistent')->first()`will
incorrectly return the non-pending tenant ('foo').

This is because `withoutPending()` does
`$builder->whereNull('data->pending_since')->orWhereNull('data')`. These
two aren't grouped, so `withoutPending()->where('id', 'nonexistent')`
basically translates to "WHERE data->pending_since IS NULL **OR (data IS
NULL AND id = 'nonexistent')**". So the query will include all tenants
whose `pending_since` is null (= all non-pending tenants).

Grouping `->whereNull('data->pending_since')->orWhereNull('data')` in a
closure passed to a separate `where()` fixes this issue.
This commit is contained in:
lukinovec 2026-04-22 14:32:53 +02:00 committed by GitHub
parent c32f52ce7c
commit ab2a4d8438
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View file

@ -58,8 +58,10 @@ class PendingScope implements Scope
{
$builder->macro('withoutPending', function (Builder $builder) {
$builder->withoutGlobalScope(static::class)
->whereNull($builder->getModel()->getColumnForQuery('pending_since'))
->orWhereNull($builder->getModel()->getDataColumn());
->where(function (Builder $query) {
$query->whereNull($query->getModel()->getColumnForQuery('pending_since'))
->orWhereNull($query->getModel()->getDataColumn());
});
return $builder;
});