Download the PHP package bakgul/laravel-query-helper without Composer
On this page you can find all versions of the php package bakgul/laravel-query-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bakgul/laravel-query-helper
More information about bakgul/laravel-query-helper
Files in bakgul/laravel-query-helper
Package laravel-query-helper
Short Description A package to add some wrapper around filtering, groupping, sorting and maybe more to write queries faster.
License
Informations about the package laravel-query-helper
Laravel Query Helper
This package aims to add a handy and quite flexible features to Laravel's Eloquent query builder.
Installation
First, install package.
Next, pulish config.
Usage
Filtering
First, you need to add IsFilterable
trait and an array of filters to each model where you want to apply filters. That array can have two keys:
- Self: the list of filters that will be applied directly to that model.
- With: an associative array of related models that can be used to filter the main model. The keys in this array must be the same as the method names of the relations.
Let's say we have the following models and relations.
When you call filter
method, it will generate a filtering array throughout the filters
array on models starting from User
recursively.
To prevent infinite loop in recursion, we stop each branch when they go back to main class after adding main class to the tree. That means You can filter by the following logic:
- Users that have cetain roles that belongs to some users.
-
This example might not be seen very usefull, but it explains the capability.
Since it's an expensive operation to construct the filtering array, we will cache it when it's created first time.
$request->filters
should be in a structure like the example down below. The important things here:
self
filters will be passed directly.with
filters will be collected underwith
key.
The example up above will filter the users based on the following list:
- its name contains 'x' or ends with 'y'
- the name of one of its roles starts with 'editor'
- the role can delete something.
Polymorphic Relationship Filter
Unlike other relationships, polymoprphic ones should be listed under the self
key of $filters
array.
The filter in request should be like this:
Groupping
Groupping operates on PHP level. If you want to group your data in the database level, you can't use this part. Otherwise, this is how to use it:
- Add
IsGrouppable
trait to the model that you want to group. - If you want to group a model with some columns all the time, add
protected static $groupKeys = ['name', 'year']
property to the model. - Then use it like so:
group
method can be found in IsGrouppable
trait as scopeGroup
and it accepts the following arguments:
- keys: the list of group keys
- take: the number of items that will be in each group. Default is zero and means 'all'
- isLast: make it true when you want to get latest records. Default is false.
- select: the array of columns that will be selected. Default is ['*'] means 'all'
- column: the name of the column when you needed. I use it for the time modifiers such as year and month. Its default value is 'created_at'
But what if you want to group users with a column that doesn't exist. You can do that thanks to modifiers that are shipped in the package and can be be found on src/Modifiers
.
The modifiers will change the sql query to add new field on the fly. For example:
The method up above will add year
and email_provider
fields to the each user. year
will be extracted from updated_at
while email_provider
from email
. Each user will contain selected 3 columns and these 2.
Modifiying
This is used by groupping functionality, and it's already explained, but just as a remainder, you can use it out of grouping too.
- Add
IsModifyable
trait to model. - call
modify
method as a part of query builder.
Sorting
It's a quite simple method that allowes you to pass all sorting columns in one method.
Extending Functionalities
Filtering
In order to extend available filters, all you need to do is to create your own filter classes and use them in models. Let's say you have a table that contains city
and need to apply a filter to it.
First create a class. The class name must be the pascal case version of the key that you will pass in request.
Or you can create your own filtering logic instead of using Text
filter.
After you create your new filter class, add it to model which can use it.
Now, you can filter addresses based on city, or users who is in that city/cities.
Groupping
If you want to use one of your current columns as they are, you don't need to take any action. Simply pass the column name in the array of group keys.
But let's say you have domain
column where you store the web sites' adresses and you want to group them beased on top-level domain (.com, .net, etc.)
First you need a modifier to extract that part and store it in a new field in query.
Then you need to add it to modifiers
array in config/query-helper.php
Now you can use it like so:
License
This is open-sourced software licensed under the MIT license.