mirror of
https://github.com/archtechx/tenancy.git
synced 2026-05-07 01:04:03 +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:
parent
c32f52ce7c
commit
ab2a4d8438
2 changed files with 16 additions and 2 deletions
|
|
@ -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;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue