PHP code example of msantang / query-filters

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

    

msantang / query-filters example snippets


  /users/?created_from=2017-01-01&created_to=2017-01-01&roles_name=Admin
  /users/?id=2
  /users/?id[]=1&id[]=3&id[]=6

#!php

Route::get('user/', function () {
    return App\User::filter()->get();
});

php artisan queryfilter:make User

#!php

Route::get('user/', function () {
    return App\User::filter()->get();
});

#!php

$mapping = [
    'id' => 'byid',
];

$filter = [
    'id' => 'numeric:eq'
]

  /users/?byid=2&byid_opt=neq

#!php

$filters = [
   'created_at' => 'date:from|date:to',
   'id'         => [function($query, $value, $name,  $opt ){
         $query->where($name, $value);
    }]
];


#!php

use Msantang\QueryFilters\Contracts\ParameterFilterInterface;
use Msantang\QueryFilters\ParameterFilter\AbstractParameterFilter;

class MyFilter extends AbstractParameterFilter implements ParameterFilterInterface
{
    public function apply($query, $value, $name,  $opt = null)
    {
        if (empty($opt)) $opt[0] = 'eq';

        switch ($opt[0]) {
            case 'eq':
                $query->where($name,'=', $value);
                break;

            default:
                $query->where($name,'like', "%$value%");
                break;
        }

    }
}