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

Laravel Model Filter Laravel Package

lacodix/laravel-model-filter

Filter, search, and sort Eloquent models with reusable filter classes and query-string support. Includes built-in types (string, date, number, enum), relation/nested relation filtering, custom complex logic, and filter visualisation.

View on GitHub
Deep Wiki
Context7

title: Introduction weight: 1

This package allows you to filter, search and sort models while fetching from database with ease. It contains additional functionality to use query strings to filter, search and sort.

Once installed you can filter, search and sort Models. You can create own filters based on our base filters, create individual filters, or use one of the extended filters, that are ready to use with less or even without configuration like TrashedFilter.

Additionally you can use the visualisation functionality of filters.

Filter

Create your first filter

php artisan make:filter CreatedAfterFilter --type=date --field=created_at
// Set the filter mode
// App\Models\Filters\CreatedAfterFilter
public FilterMode $mode = FilterMode::GREATER_OR_EQUAL;

// Apply this filter and the HasFilters trait to a Model
// App\Models\Post
use HasFilters;
protected array $filters = [
    CreatedAfterFilter::class,
];

// Somwhere in a controller, select all posts created after 1st of January 2023
Post::filter(['created_after_filter' => '2023-01-01'])->get();

// Do the same via query string by calling
// this url: https://.../posts?created_after_filter=2023-01-01
Post::filterByQueryString()->get();

Visualize

All filters have a blade template that can visualize the filter with one or multiple input fields. To visualize all filters of a dedicated model you can use a blade component:

<x-lacodix-filter::model-filters :model="Post::class" />

Grouping

Sometimes you don't need all of the filters for all parts of a web application. Maybe there shall be different filters be available to the backend as in the frontend, or different user types shall be able to use different filters.

For such cases this package offers filter grouping when adding filters to models

protected array $filters = [
    'frontend' => [
        HotFilter::class,
    ],
    'backend' => [
        CreatedAfterFilter::class,
        PublishedFilter::class,
    ]
];

The groups can be used in the scopes

Post::filterByQueryString('frontend')->get()

or

Post::filter(['hot_filter' => 'hot'], 'frontend')->get();
Post::filter(['created_after_filter' => '2023-01-01'], 'backend')->get();

Search

// add searchable fields and the IsSearchable trait to Model:
// App\Models\Post
use IsSearchable;
protected array $searchable = [
    'title',
    'content',
];

// Somewhere in controller, find all posts that contain "test" in title or content
Post::search('test')->get();

// Do the same via query string by calling
// this url: https://.../posts?search=test
Post::searchByQueryString()->get();

Sorting

// add sortable fields and the IsSortable trait to Model:
// App\Models\Post
use IsSortable;
protected array $sortable = [
    'title',
    'created_at',
    'content',
];

// Somewhere in controller, get all post sorted by title in descending order
Post::sort(['title' => 'desc'])->get();

// Do the same via query string by calling
// this url: https://.../posts?sort[title]=desc
Post::sortByQueryString()->get();

We have badges

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui