This package allows you to filter, sort, append, paginate and load relations based on the request query parameters.
You can install the package via composer:
composer require wpzag/laravel-query-builder
You can publish the config file with:
php artisan vendor:publish --tag="query-builder-config"
'models' => [
User::class => [
'includes' => ['posts.comments.author', ],
'appends' => ['fullname','avatar'],
'filterable' => ['*:except:role', 'posts.title', 'posts.body', 'posts.created_at'],
'sortable' => ['*'],
'max_per_page' => 20
],
]
[ * ] means all the model's attribute except hidden .
[ *:except:column1,column2 ] You can also exclude some attributes by adding :except: then comma separated attributes .
[ age:exact ] If you prefixed the attribute with :extact it will use the ( = ) operator instead of ( LIKE ) operator in the query.
$builder = QueryBuilder::for(User:class); //This applies includes/filters/sorts to the query
$builder->query() // Returns the query
$builder->get() // Returns the eloquent collection
$builder->withPagination() // Applies pagination to the query
$builder->withAppends() // Applies appends to the query
$builder->withPaginationAndAppends() // Applies pagination & appends to the query
QueryBuilder::for(User:class)->query()->where('id', '>', 1)->get();
// Or if you are using withPagination(),withAppends() or withPaginationAndAppends() :
QueryBuilder::for(User:class, function($query){
//extra queries here
return $next($query);
})->withPaginationAndAppends();
users?filter[name]=john,rose
// User::where('name','like','%john%')
// ->orWhere('name','like','%rose%')->get();
users?filter[name,username]=john
// User::where('name','like','%john%')
// ->orWhere('username','like','%rose%')->get();
users?filter[name]=john,rose&filter[age]=gt.20
// User::where('name','like','%john%')
// ->orWhere('name','like','%rose%')
// ->where('age','>',20)->get();
users?filter[age][between]=[29,40]
// User::whereBetween('age', [29, 40])->get();
users?filter[email_verified_at][empty]
// User::whereNull('email_verified_at')->get();
users?filter[created_at][date]=2020-01-01
// User::whereDate('created_at', '2020-01-01')->get();
users?filter[posts.created_at]=lt.2020-01-01
// User::whereHas('posts', function($query) {
// $query->whereDate('created_at', '<', '2020-01-01');
// })->get();
users?filter[role][not-in]=[admin,teacher,student]
// User::whereNotIn('role', ['admin', 'teacher', 'student'])->get();
users?sort=-created_at
// User::orderBy('created_at', 'desc')->get();
users?sort=-statues,name
// User::orderBy('statues', 'desc')->orderBy('name', 'asc')->get();
users?append=fullname
// User::get()->append('fullname')
users?include=profile,posts.comments.author
// User::with('profile','posts.comments.author')->get();
users?page=2&limit=10
// User::paginate(10);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.