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: Filter Visualisation weight: 5

To use your filters in your web application you have to offer the filter options to your visitors. You can just use the query parameters or create your own forms and input fields, but for convenience this packet ships with a set of views for each filter-type. Each view creates a simple input with headline.

Filter form

Additionally it comes with a blade component for integration of all filters of a model.

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

you can set the model via a class name string, or via an instance of the model.

<x-lacodix-filter::model-filters :model="$post" />

This will result in the following HTML code given the following example classes below

<form method="get">
    <div class="filter-container select">
        <div class="filter-title">
            Post Type Filter
        </div>

        <div class="filter-content">
            <select class="filter-input" name="post_type_filter" onchange="this.form.submit()">
                <option value="">&mdash;</option>
                <option value="page">page</option>
                <option value="post">post</option>
            </select>
        </div>
    </div>
</form>
File: App\Models\Filters\PostTypeFilter  
<?php

namespace App\Models\Filters;

use Lacodix\LaravelModelFilter\Filters\SelectFilter;

class PostTypeFilter extends SelectFilter
{
    protected string $field = 'type';

    public function options(): array
    {
        return [
            'page',
            'post',
        ];
    }
}



File: App\Models\Post  
<?php

namespace App\Models;

use App\Models\Filters\PostTypeFilter;
use Illuminate\Database\Eloquent\Model;
use Lacodix\LaravelModelFilter\Traits\HasFilters;

class Post extends Model
{
    use HasFilters;

    protected array $filters = [
        PostTypeFilter::class,
    ];
}

Form attributes

You can also add attributes to the form and change the method, if needed, like so:

<x-lacodix-filter::model-filters :model="Post::class" method="post" :action="route('posts')" />

Change headline

To change the headline of the filter visualisation just overwrite the $title property of the filter or if you need translations you can also overwrite the title()-Method.

    protected string $title = 'My Filter';
    public function title(): string
    {
        return __('My Filter');
    }

CSS styles for filter

You can style all filters with the following classes:

.filter-container is the surrounding div of any filter type. Cou can specify the filter type with the corresponding classes like boolean, select, text, numeric, date: e.g.: .filter-container.select

.filter-container {

}
.filter-container.select {
    
}

more available classes are filter-title, filter-content and filter-input that addresses the corresponding div or input element in the views

.filter-title,
.filter-content {
    width: 100%;
}

.filter-input {
    width: 100%;
    padding: 5px;
}

Change available views

You can publish the views of this package and change all filter blade files for your needs. See installation section for more information.

Using own components

You can change the component used by a filter, by overwriting a filters $component property. If you for example create an individual filter you can decide if it will use one of the given components, or you can also select an individual component with one of the following options:

    ...
    protected string $component = 'select';

This will search for a select-component in the packages components filters views folder: resources\views\vendor\lacodix-filter\components\filters\select.blade.php

    ...
    public function component(): string
    {
        return 'my-component';
    }

This will use the individual component for the filters view.

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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
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