In Laravel, we commonly face the problem of adding repetitive filtering code, this package will address this problem.

Overview

Filterable

Software License Total Downloads

In Laravel, we commonly face the problem of adding repetitive filtering code, sorting and search as well this package will address this problem.

Install

composer require kalimeromk/filterable

Usage for Filtering property

To use Filterable trait we need to include trait into our model

Use Filterable;

While adding Filterable trait in the model class, we need to add some properties as well.

$fillable: Specify all the fields which exist in your table.
$boolFields:- Add fields on which you want to apply Boolean filtering.

Examples

        protected array $fillable = [
            'name',
            'email',
            'address',
        ];
        protected array $boolFields = [
            'is_active',
        ];

After all the above changes, now we only need to call filter() function with Request array data

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
	protected $model;
  
	public function __construct(User $model)
	{
		$this->model = $model;
	}
  
	public function index(Request $request)
	{
		$users = $this->model
                  ->filter($request->all())
                  ->get();
		
		return view('users.index', compact('users'));
	}
}

Usage for Sort property

To use Sortable trait we need to include trait into our model

Use Sortable;

While adding Sortable trait in the model class, we need to add some properties as well.

$sortable:- Add fields on which you want to apply sort property.

Examples

        protected array $sortable = [
            'id',
            'name',
            'email',
            'address'
        ];

After all the above changes, now we only need to call sort() function with Request array data

Example below allows sorting for the columns: id, name, email, address

<?php

namespace App;

use Kalimeromk\Filterable\Trait\Sortable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Sortable;

    public $sortables = ['id', 'name', 'email', 'address'];
}

Trait usage

Below is an example of the usage of the sortable trait (query scope).

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
	protected $model;
  
	public function __construct(User $model)
	{
		$this->model = $model;
	}
  
	public function index(Request $request)
	{
		$users = $this->model
                  ->sort($request->all())
                  ->get();
		
		return view('users.index', compact('users'));
	}
}

Usage for whereLike property

To use whereLike search need first to specified all the table row you want to search try

$likeRows:- Add fields on which you want to apply whereLike property.

Examples

    public const likeRows = [
        'name',
        'email',
        'address'
   ];

After all the above changes, now we only need to call whereLike() function with Request array data

Example below allows searching for the columns: name, email, address

<?php

namespace App;

use Kalimeromk\Filterable\Trait\Sortable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public const likeRows = [
        'name',
        'email',
        'address'
        ];
}

this method can be used to search try relation as well just update const with relation and row name for search

 public const likeRows = [
        'name',
        'email',
        'address',
        'country.name'
        ];

whereLike usage

Below is an example of the usage of the whereLike method (query scope).

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
	protected $model;
  
	public function __construct(User $model)
	{
		$this->model = $model;
	}
  
	public function index(Request $request)
	{
		$users = $this->model
                  ->whereLike(User::likeRows, Arr::get($request, 'search'))
                  ->get();
		
		return view('users.index', compact('users'));
	}
}

NOTE: This also works with filter, sort and with pagination

$users = $this->model->whereLike(User::likeRows, Arr::get($request, 'search'))->sort($request->all())->filter($request->all())->paginate(10)

Testing

Run the tests with:

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

Security

If you discover any security-related issues, please email [email protected] or use the issue tracker.

License

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

You might also like...
A small package for adding UUIDs to Eloquent models.

A small package for adding UUIDs to Eloquent models. Installation You can install the package via composer: composer require ryangjchandler/laravel-uu

Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.

Laravel Eloquent Scope as Select Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes an

This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.

Laravel Verify New Email Laravel supports verifying email addresses out of the box. This package adds support for verifying new email addresses. When

Simple laravel hook for adding meta tags to head for inertia

laravel seo hook for js frameworks simple hook for adding meta tags to head/head for js frameworks inertia:react,vue, etc... in app/Meta.php put M

Simple address and contact management for Laravel with automatically geocoding to add longitude and latitude

Laravel Addresses Simple address and contact management for Laravel with automatically geocoding to add longitude and latitude. Installation Require t

EmailValidator - PHP Email address validator

EmailValidator A library for validating emails against several RFC. Supported RFCs This library aims to support RFCs: 5321, 5322, 6530, 6531, 6532, 10

How to get cookies from users' browser and send the information to your email address and telegram bot

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

🔌  Convert Bootstrap CSS code to Tailwind CSS code
🔌 Convert Bootstrap CSS code to Tailwind CSS code

Tailwindo This tool can convert Your CSS framework (currently Bootstrap) classes in HTML/PHP (any of your choice) files to equivalent Tailwind CSS cla

laravel-vat is a package that contains the Laravel related wiring code for ibericode/vat

laravel-vat is a package that contains the Laravel related wiring code for ibericode/vat, helping you deal with VAT legislation for businesses based in the EU.

Releases(v1.1)
Owner
Zoran Shefot Bogoevski
Senior Full Stack Software Engineer
Zoran Shefot Bogoevski
A multitool library offering access to recommended security related libraries, standardised implementations of security defences, and secure implementations of commonly performed tasks.

SecurityMultiTool A multitool library offering access to recommended security related libraries, standardised implementations of security defences, an

Pádraic Brady 131 Oct 30, 2022
🥳🔐 This package is a Laravel package that checks if an email address is a spammer

This package is a Laravel package that checks if an email address is a spammer. It verifies your signups and form submissions to confirm that they are legitimate.

Endurance, the Martian 15 Dec 19, 2022
Advanced Laravel models filtering capabilities

Advanced Laravel models filtering capabilities Installation You can install the package via composer: composer require pricecurrent/laravel-eloquent-f

Andrew Malinnikov 162 Oct 30, 2022
A base API controller for Laravel that gives sorting, filtering, eager loading and pagination for your resources

Bruno Introduction A Laravel base controller class and a trait that will enable to add filtering, sorting, eager loading and pagination to your resour

Esben Petersen 165 Sep 16, 2022
Gretel is a Laravel package for adding route-based breadcrumbs to your application.

Gretel Laravel breadcrumbs right out of a fairy tale. Gretel is a Laravel package for adding route-based breadcrumbs to your application. Defining Bre

Galahad 131 Dec 31, 2022
A Laravel package for quickly adding .well-known URLs

A Laravel package for quickly adding .well-known URLs well-known is a Laravel package for quickly adding well-known locations (RFC8615) to a Laravel a

Kim Hallberg 2 Sep 13, 2022
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.

Laravel Befriended Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked

Renoki Co. 720 Jan 3, 2023
Library that offers Input Filtering based on Annotations for use with Objects. Check out 2.dev for 2.0 pre-release.

DMS Filter Component This library provides a service that can be used to filter object values based on annotations Install Use composer to add DMS\Fil

Rafael Dohms 89 Nov 28, 2022
A simple and modern approach to stream filtering in PHP

clue/stream-filter A simple and modern approach to stream filtering in PHP Table of contents Why? Support us Usage append() prepend() fun() remove() I

Christian Lück 1.5k Dec 29, 2022
This project uses dflydev/dot-access-data to provide simple output filtering for cli applications.

FilterViaDotAccessData This project uses dflydev/dot-access-data to provide simple output filtering for applications built with annotated-command / Ro

Consolidation 44 Jul 19, 2022