PHP code example of alifcoder / query-filter

1. Go to this page and download the library: Download alifcoder/query-filter 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/ */

    

alifcoder / query-filter example snippets


return [

    // Validation rules for the built-in query string parameters
    // (sort, limit, search, pagination, soft-delete toggles, ...).
    'default_filters' => [ /* ... */ ],

    // Logical field name => actual database column name, used by
    // BaseEBFilter's prefix(), index(), isActive(), deletedAt(), createdAt(),
    // updatedAt(), createdBy(), updatedBy() and by the default search/sort
    // fields below. Override a value if your table uses a different column
    // name — no need to override the base class.
    'columns' => [
        'prefix'        => 'prefix',
        'index'         => 'index',
        'is_active'     => 'is_active',
        'deleted_at'    => 'deleted_at',
        'created_at'    => 'created_at',
        'updated_at'    => 'updated_at',
        'created_by_id' => 'created_by_id',
        'updated_by_id' => 'updated_by_id',
        // ...
    ],

    // Field keys automatically registered as searchable/sortable for every
    // filter, on top of whatever searchFields()/sortFields() return.
    'default_search_fields' => ['prefix-index', 'prefix', 'index', /* ... */],
    'default_sort_fields'   => ['prefix-index', 'index', 'prefix', /* ... */],

    // Relations resolved by checkJoin() and used to build the
    // "created_by.name" / "updated_by.name" default fields above.
    'default_joins' => [
        'created_by' => [
            'table'        => 'users as created_by',
            'first'        => 'created_by.id',
            'second'       => '{table}.created_by_id',
            'alias'        => 'created_by',
            'name_columns' => ['first_name', 'last_name'],
        ],
        // ...
    ],

    // Extra ValidationRuleDTO definitions merged into every FormRequest that
    // uses FilterPrepareForRequestTrait::getFields().
    'default_validation_fields' => [
        ['field' => 'prefix', 'rules' => ['string'], 'operations' => ['eq', 'ne']],
        ['field' => 'index', 'rules' => ['string'], 'operations' => 'all'],
        // ...
    ],
];

// app/Filters/PostFilter.php
namespace App\Filters;

use Alif\QueryFilter\Abstracts\BaseEBFilter;
use Alif\QueryFilter\Interfaces\Searchable;
use Illuminate\Database\Eloquent\Builder;

class PostFilter extends BaseEBFilter implements Searchable
{
    protected string $table = 'posts';

    protected function getCallback(): array
    {
        return [
            'is_active'     => [$this, 'isActive'],
            'created_at'    => [$this, 'createdAt'],
            'created_by_id' => [$this, 'createdBy'],
            'search'        => [$this, 'search'],
            'sort'          => [$this, 'sort'],
            'limit'         => [$this, 'limit'],
        ];
    }

    protected function sortFields(): array
    {
        return [
            'title' => $this->table . '.title',
        ];
    }

    protected function joinTables(): array
    {
        return [];
    }

    public function searchFields(string $search): array
    {
        return [
            'title' => $this->table . '.title',
        ];
    }
}

use Alif\QueryFilter\Traits\Filterable;

class Post extends Model
{
    use Filterable;
}

use App\Filters\PostFilter;

$posts = Post::filter(new PostFilter($request->validated()))->get();

use Alif\QueryFilter\DTO\ValidationRuleDTO;
use Alif\QueryFilter\Traits\FilterPrepareForRequestTrait;
use Illuminate\Foundation\Http\FormRequest;

class PostIndexRequest extends FormRequest
{
    use FilterPrepareForRequestTrait;

    public function fields(): array
    {
        return [
            new ValidationRuleDTO('title', ['string']),
        ];
    }
}
bash
php artisan vendor:publish --tag=query-filter
bash
php artisan query-filter:uninstall