Here we are going to use Laravel purity PHP package for filtering and sorting. To see the package repo Click Here If we use this, it create a common query pattern for frontend developer so that they can easily execute filtering and sorting in server end using Query params. So that development becomes easier ans faster.
Create laravel application using.
composer create-project laravel/laravel test-filter-and-sort-app
Make Migrations, Model and controller for User and Post table by running those commends..
php artisan make:model User -mc
php artisan make:model Post -mc
Demo codes are given in link.
php artisan migrate
php artisan serve
need to run those commend in laravel application dir.
composer require abbasudo/laravel-purity
php artisan vendor:publish --tag=purity
Those commend will instal pakckage in vendor folder and published config file (configs/purity.php)
where we can customize package’s behavior.
Just only need to add Filterable
trait to your model to get filters functionalities.
use Abbasudo\Purity\Traits\Filterable;
class User extends Model
{
use Filterable;
}
Now add filter()
to your model eloquent query in the controller.
Post::filter()->get();
Request
Formet ?filters[field][operator]=value
Example ?filters[name][$in]=Shahin
Just only need to add Sortable
trait to your model to get filters functionalities.
use Abbasudo\Purity\Traits\Filterable;
class User extends Model
{
use Sortable;
}
Now add sort()
to your model eloquent query in the controller.
Post::sort()->get();
Request
Formet ?sort=value:type
Example ?sort=created_at
NB - By default its ascending.
Example ?sort=created_at:desc
This laravel Purity pacakage is compatible with the popular JavaScript Package QS
Using this frontend developers can effortlessly generate APIs query params for filtering and sorting..
Commend : npm i qs
const qs = require('qs');
const query = qs.stringify({
filters: {
name: {
$in: 'Shahin',
},
},
}, {
encodeValuesOnly: true, // prettify URL
});
console.log(query)
// result = ?filters[name][$in]=Shahin
await request(`/api/users?${query}`);
const qs = require('qs');
const query = qs.stringify({
sort:'created_at:desc'
}, {
encodeValuesOnly: true, // prettify URL
});
console.log(query)
// result = ?sort=created_at:desc
await request(`/api/users?${query}`);
Operator | Description |
---|---|
$eq | Equal |
$eqc | Equal (case-sensitive) |
$ne | Not equal |
$lt | Less than |
$lte | Less than or equal to |
$gt | Greater than |
$gte | Greater than or equal to |
$in | Included in an array |
$notIn | Not included in an array |
$contains | Contains |
$notContains | Does not contain |
$containsc | Contains (case-sensitive) |
$notContainsc | Does not contain (case-sensitive) |
$null | Is null |
$notNull | Is not null |
$between | Is between |
$startsWith | Starts with |
$startsWithc | Starts with (case-sensitive) |
$endsWith | Ends with |
$endsWithc | Ends with (case-sensitive) |
$or | Joins the filters in an “or” expression |
$and | Joins the filters in an “and” expression |