Skip to content

AhnabShahin/filter-and-sort-in-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Filter and Sort in Laravel

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.

Table of Contents

How To Use

Laravel Setup

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

Laravel Purity Installation

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.

Filter

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

Sort

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

Frontend setup

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

Filter
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}`);
Sort
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}`);

Advance usage

Available Oparation for Filtering

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages