PHP code example of hashemi / queryfilter

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

    

hashemi / queryfilter example snippets


composer 

Hashemi\QueryFilter\QueryFilterServiceProvider::class

$app->register(Hashemi\QueryFilter\QueryFilterServiceProvider::class)

class User extends Model
{
    // Use Filterable Trait
    // ....
    use \Hashemi\QueryFilter\Filterable;
    // ....
}

php artisan make:filter UserFilter

class UserFilter extends \Hashemi\QueryFilter\QueryFilter
{
    public function applyIdProperty($id)
    {
        return $this->builder->where('id', '=', $id);
    }

    public function applyNameProperty($name)
    {
        return $this->builder->where('name', 'LIKE', "%$name%");
    }
}

class UserController extends Controller
{
    public function index(Request $request, UserFilter $filter)
    {
        $user = User::query()->filter($filter)->get();
        // do whatever
    }
}

class UserController extends Controller
{
    public function index(Request $request, UserFilter $filter)
    {
        $user = User::query()->filter($filter, [
            'username' => 'ssi-anik'
        ])->get();
        // do whatever
    }
}


class UserFilter extends \Hashemi\QueryFilter\QueryFilter
{
    public function applyIdProperty($id)
    {
        return $this->builder->where('id', '=', $id);
    }

    public function applyNameProperty($name)
    {
        return $this->builder->where('name', 'LIKE', "%$name%");
    }
    
    public function applyUsernameProperty($username)
    {
        return $this->builder->where('username', 'LIKE', "%$username%");    
    }

}