PHP code example of mehradsadeghi / laravel-filter-querystring

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

    

mehradsadeghi / laravel-filter-querystring example snippets


$users = User::latest();

if(request('username')) {
    $users->where('username', request('username'));
}

if(request('age')) {
    $users->where('age', '>', request('age'));
}

if(request('email')) {
    $users->where('email', request('email'));
}

return $users->get();


use Mehradsadeghi\FilterQueryString\FilterQueryString;

class User extends Model
{
    use FilterQueryString;

    protected $filters = [];

    ...
}

User::select('name')->filter()->get();

User::filter()->get();

protected $filters = ['sort'];

protected $filters = [
    'greater',
    'greater_or_equal',
    'less',
    'less_or_equal',
    'between',
    'not_between'
];

protected $filters = ['in'];

protected $filters = ['like'];

protected $filters = ['name', 'username', 'age'];

protected $filters = ['all_except'];

public function all_except($query, $value) {
    return $query->where('name', '!=', $value);
}

protected $filters = ['in'];

public function in($query, $value) {
    
    $exploded = explode(',', $value);

    if(count($exploded) != 4) {
        // throwing an exception or whatever you like to do
    }

    $field = array_shift($exploded);

    return $query->whereIn($field, $exploded);
}

protected $filters = ['by'];

public function by($query, $value) {
    return $query->where('username', $value);
}

User::filter('in')->get();

User::filter('like', 'name')->get();