Looking to hire Laravel developers? Try LaraJobs

laravel-query-builder maintained by wpzag

Description
Build up Eloquent queries from the Query string parameters of the request .
Author
Last update
2023/02/18 22:24 (dev-main)
License
Downloads
11

Comments
comments powered by Disqus

Build up Eloquent queries from the Query string parameters of the reuqest .

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package allows you to filter, sort, append, paginate and load relations based on the request query parameters.

Installation

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"

Usage

Add config for your model in query-builder.php

'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.

Usage


$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

You can also add custom query to query-builder


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();

Examples:

Filtering

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();

Sorting

users?sort=-created_at

// User::orderBy('created_at', 'desc')->get();

users?sort=-statues,name

// User::orderBy('statues', 'desc')->orderBy('name', 'asc')->get();

Appending

users?append=fullname

// User::get()->append('fullname')

Loading relations

users?include=profile,posts.comments.author

// User::with('profile','posts.comments.author')->get();

Pagination

users?page=2&limit=10

// User::paginate(10);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.