PHP code example of janakkapadia / filterable

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

    

janakkapadia / filterable example snippets


    use JanakKapadia\Filterable\Traits\Filter;
    
    class YourModel extends Model
    {
        use Filter;
    
        // Additional model code...
    }
    

    public function index(Request $request)
    {
        $data = YourModel::filter()->get();
    }
    

    public function index(Request $request)
    {
        $data = YourModel::filter(true)->get();
    }
    

    class YourModelFilters extends Filter
    {
        // Define your filterable fields
        protected array $filters = ['search', 'status'];
        
        // Define your sortable fields
        protected array $sort = ['sort_by'];
   
        // Sort method
        public function sort_by($column): void
        {
            $this->builder->orderBy($column);
        }
   
        // Filter methods
        public function search($keyword): void
        {
            $this->builder->where(function ($query) use ($keyword) {
                $query->where('field1', 'like', "%$keyword%")
                    ->orWhere('field2', 'like', "%$keyword%");
            });
        }

        public function status($status): void
        {
            $this->builder->where('status', $status);
        }
    }
    
bash
    php artisan filter:model {ModelName}