Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Eloquent Filtering Laravel Package

indexzer0/eloquent-filtering

Define allowed filters on your Eloquent models and apply them from simple arrays or request data—no custom query logic. Supports complex, type-based filtering for APIs and dashboards on Laravel 10+ / PHP 8.2+.

View on GitHub
Deep Wiki
Context7

title: 'Required Filters' version: 'v2' icon: 'asterisk' iconType: 'solid'

Overview

You can specify that Filter::field(), Filter::relation(), Filter::morphRelation(), Filter::morphType() and Filter::custom() filters must be required.

  • When a required filter is not used, a RequiredFilterException is thrown.
  • RequiredFilterException extends Laravels ValidationException.
    • You can let this bubble up to your controller for the default laravel 422 response.
  • This exception CAN NOT be suppressed.

Marking As Required

public function allowedFilters(): AllowedFilterList
{
    return Filter::only(
        /*
         * Field
         */
        Filter::field('name', [FilterType::LIKE])->required(),
        /*
         * Relation
         */
        Filter::relation('books', [FilterType::HAS],
            Filter::only(
                Filter::field('title', [FilterType::LIKE])->required()
            )
        )->required(),
        /*
         * Morph Relation
         */
        Filter::morphRelation('imageable', [FilterType::HAS_MORPH],
            /*
             * Morph Type
             */
            Filter::morphType(Article::class,
                Filter::only(
                    Filter::field('title', [FilterType::LIKE])->required()
                )
            )->required()
        )->required(),
        /*
         * Custom
         */
        Filter::custom('$latest')->required()
    );
}

Model::filter([]);
// RequiredFilterException errors
[
    'name' => [
        'Name filter is required.',
    ],
    'books' => [
        'Books filter is required.',
    ],
    'books.title' => [
        'Title filter is required.',
    ],
    'imageable' => [
        'Imageable filter is required.',
    ],
    'imageable.articles' => [
        'Articles filter is required.',
    ],
    'imageable.articles.title' => [
        'Title filter is required.',
    ],
    '$latest' => [
        '$latest filter is required.',
    ],
]

Relaxing The Required Scope

  • Sometimes you may want a filter to be required ONLY if it's parent has been filtered.
    • You can set the scoped parameter to true to achieve this.
public function allowedFilters(): AllowedFilterList
{
    return Filter::only(
        Filter::relation('books', [FilterType::HAS],
            Filter::only(
                Filter::field('title', [FilterType::LIKE])
                    ->required(scoped: true)
            )
        ),
    );
}

Model::filter([]);
// 'books' relation filter not used
// RequiredFilterException not thrown.

Model::filter([
    [
        'target' => 'books',
        'type' => '$has',
    ]
]);
// 'books' relation filter used
// RequiredFilterException errors
[
    'books.title' => [
        'Title filter is required.',
    ],
]
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium