PHP code example of grantholle / laravel-model-filters

1. Go to this page and download the library: Download grantholle/laravel-model-filters library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

grantholle / laravel-model-filters example snippets


use GrantHolle\ModelFilters\Enums\Component;
use GrantHolle\ModelFilters\Filters\MultipleSelectFilter;
use GrantHolle\ModelFilters\Filters\TextFilter;
use GrantHolle\ModelFilters\Traits\HasFilters;

class User extends Authenticatable implements ExistsInSis
{
    use HasFilters;
    
    public function filters(): array
    {
        return [
            TextFilter::make('search', __('Search'))
                // Exclude from the list of available filters (see below when not present on `availableFiltersToArray()`
                ->hide()
                // By default, the filter will try to construct the query based on the supplied operator and value.
                // If that doesn't meet your needs, you can define the query parameters yourself. It should
                // return an instance of `Illuminate\Database\Eloquent\Builder`.
                ->using(fn (Builder $builder, string $search) => $builder->search($search)),
            // The first argument is the key that will be used to filter the model. The second argument is the label    
            TextFilter::make('first_name', __('First name')),
            TextFilter::make('last_name', __('Last name')),
            MultipleSelectFilter::make('user_type', __('Checkbox group'))
                // For filters that can have multiple values/choices, you can
                // define the options. How it's constructed is up to you, since
                // the frontend is implemented independently.
                ->options(UserType::options()),
            MultipleSelectFilter::make('user_type', __('Combobox'))
                ->withComponent(Component::combobox)
                ->options(UserType::options()),
        ];
    }
}

(new User())->availableFiltersToArray();

// This is the output
$filters = [
    [
        "key" => "first_name",
        "label" => "First name",
        "component" => "text",
        "operators" => [
            "contains" => "Contains",
            "not_contains" => "Doesn't contain",
            "starts" => "Starts with",
            "not_starts" => "Doesn't start with",
            "ends" => "Ends with",
            "not_ends" => "Doesn't end with",
        ],
        "props" => [],
        "defaultValue" => null,
    ],
    [
        "key" => "last_name",
        "label" => "Last name",
        "component" => "text",
        "operators" => [
            "contains" => "Contains",
            "not_contains" => "Doesn't contain",
            "starts" => "Starts with",
            "not_starts" => "Doesn't start with",
            "ends" => "Ends with",
            "not_ends" => "Doesn't end with",
        ],
        "props" => [],
        "defaultValue" => null,
    ],
    [
        "key" => "user_type",
        "label" => "Checkbox group",
        "component" => "checkbox_group",
        "operators" => [
            "in" => "In",
            "not_in" => "Not in",
        ],
        "props" => [
            "options" => [
                "staff" => "Staff",
                "guardian" => "Contact",
                "student" => "Student",
            ],
        ],
        "defaultValue" => [],
    ],
    [
        "key" => "user_type",
        "label" => "Combobox",
        "component" => "combobox",
        "operators" => [
            "in" => "In",
            "not_in" => "Not in",
        ],
        "props" => [
            "options" => [
                "staff" => "Staff",
                "guardian" => "Contact",
                "student" => "Student",
            ],
        ],
        "defaultValue" => [],
    ],
    [
        "key" => "user_type",
        "label" => "Select",
        "component" => "combobox",
        "operators" => [
            "in" => "In",
            "not_in" => "Not in",
        ],
        "props" => [
            "options" => [
                "staff" => "Staff",
                "guardian" => "Contact",
                "student" => "Student",
            ],
        ],
        "defaultValue" => [],
    ],
];

[
    [
        "key" => "first_name",
        "operator" => "starts",
        "value" => "gr"
    ]
]

public function index(Request $request)
{
    $filters = $request->currentFilters();
    $users = User::filter($filters)
        ->get();
    
    // ...
}

use GrantHolle\ModelFilters\Enums\Operator;

$filters = [
    [
        "key" => "first_name", // The key should match the key in the filter definition
        "operator" => "contains", // You can also use the Operator::contains enum
        "value" => "an" // This is the value by which to filter
    ],
    [
        "key" => "first_name",
        "operator" => Operator::not_starts_with,
        "value" => "Gr"
    ]
];

User::filter($filters)->pluck("first_name");