PHP code example of 7amoood / eloquent-filter

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

    

7amoood / eloquent-filter example snippets


use Hamoood\EloquentFilter\HasFilter;

class Post extends Model
{
    use HasFilter;

    protected array $sortCols         = ['created_at', 'title'];
    protected array $filterSearchCols = ['title', 'body'];
    protected array $filterCols       = ['status', 'category_id'];
    protected int   $filterLimit      = 50;
}

$posts = Post::query()->filter()->paginate();

protected array $filterColsChilds = [
    'tags' => ['slug', 'name'],
];

protected array $filterCols = [
    'type' => 'category_id',  // ?filter[column][type]=5  queries category_id
    'status',                  // ?filter[column][status]=active  queries status
];

return [
    // Request parameter key containing filter data
    'request_key' => 'filter',

    // Max rows when model has no $filterLimit (null to disable)
    'default_limit' => 1000,

    // Default sort direction
    'default_sort_direction' => 'asc',
];

Post::query()->filter(key: 'f')->paginate();
// reads from ?f[search]=...

Post::query()->sortCols(['views', 'likes'])->filter()->paginate();
bash
php artisan vendor:publish --tag=eloquent-filter