PaulJasiul
1 month ago

How to use multiple filters in Spatie Query Builder

Posted 1 month ago by PaulJasiul

Hello, I have a problem with multiple filters when using Spatie Query Builder. When both filters have $query->with(...) then the last one overrides previous. Any ideas how to merge then or fix it?

Allowed filters:

$this->allowedFilters([
            AllowedFilter::custom('date', new FiltersEventDate),
            AllowedFilter::custom('theaters', new FiltersMovieTheater),
        ]);

Date Filter:

$date = (new Carbon($value))->toDateString();

        $query
            ->whereHas('events', function (Builder $query) use ($date) {
                return $query->whereDate('starts_at', $date);
            })
            ->with(['events' => function ($query) use ($date) {
                return $query->whereDate('starts_at', $date);
            }]);

Theater Filter:

$query
            ->whereHas('events', function (Builder $query) use ($value) {
                return $query->whereHas('theaterHall', function (Builder $query) use ($value) {
                    return $query->whereHas('theater', function (Builder $query) use ($value) {
                        return $query->whereIn('id', $value);
                    });
                });
            })
            ->with(['events' => function ($query) use ($value) {
                return $query->whereHas('theaterHall', function ($query) use ($value) {
                    return $query->whereHas('theater', function ($query) use ($value) {
                        return $query->whereIn('id', $value);
                    });
                });
            }]);

Please sign in or create an account to participate in this conversation.