PHP code example of emmadonjo / laravel-filter

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

    

emmadonjo / laravel-filter example snippets


    composer 


    ...

    use Emmadonjo\LaravelFilter\Contracts\Filterable;
    use Emmadonjo\LaravelFilter\Concerns\HasFilter;

    class Post extends Model implements Filterable
    {
        use HasFilter;

        public function filterableColumns(): array
        {
            return [
                'slug',
                'author_id',
                'status'
            ];
        }
    }


    // filter posts
    $filters = ['author_id' => 1, 'status' => 'published'];

    Post::filter($filters)->get();

    // filter a post's column with multiple possible values
    $filters = ['status' => ['scheduled', 'draft']];

    Post::filter($filters)->get();

    // combine both
    $filters = [
        'status' => ['scheduled', 'draft'],
        'author_id' => 1
    ];

    Post::filter($filters)->get();


    ...

    use Emmadonjo\LaravelFilter\Contracts\Searchable;
    use Emmadonjo\LaravelFilter\Concerns\HasSearch;

    class Post extends Model implements Searchable
    {
        use HasSearch;

        public function searchableColumns(): array
        {
            return [
                'slug',
                'content',
            ];
        }
    }

    $searchTerm = "hello"

    Post::search($searchTerm)->get();


    ...

    use Emmadonjo\LaravelFilter\Contracts\Filterable;
    use Emmadonjo\LaravelFilter\Contracts\Searchable
    use Emmadonjo\LaravelFilter\Concerns\HasFilter;
    use Emmadonjo\LaravelFilter\Concerns\HasSearch;

    class Post extends Model implements Filterable, Searchable
    {
        use HasFilter;
        use HasSearch

        public function filterableColumns(): array
        {
            return [
                'slug',
                'author_id',
                'status',
            ];
        }
        
        public function searchableColumns() : array
        {
             return [
                 'title',
                 'content',
            ]
        }
    }


    $filters = ['author_id' => 1, 'status' => 'published'];

    Post::filter($filters)
        ->search("hello")
        ->get();

    // filter a post's column with multiple possible values while applying search
    $filters = ['status' => ['scheduled', 'draft']];

    Post::search('hello')
        ->filter($filters)
        ->get();