Skip to content

Laravel Eloquent Filter is a package for filtering model data.

Notifications You must be signed in to change notification settings

htetoozin/laravel-eloquent-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Laravel Eloquent Filter Package

📥 Installation

You can install this package via composer using this command:


composer require htetoozin/eloquent-filter

Usage

Run php artisan make:filter {name} this file generate and stored at the location of app/Filters/. eg..

php artisan make:filter UserFilter

will generate such file content of UserFilter.php

Important

Array value and the method name must be the same; otherwise, the filter will not work.

<?php

namespace App\Filters;

use HtetOoZin\EloquentFilter\Filters\Filter;

class UserFilter extends Filter
{
    /**
     * Register filter properties
     */
    protected $filters = ['keyword'];


    /**
     * Filter by keyword.
     */
    public function keyword($value)
    {
        return $this->builder->where('name', 'like', "%{$value}%");
    }
}

Add local query scope in related model.

<?php

class User extends Model
{
    public function scopeFilter($query, $filter)
    {
        $filter->apply($query);
    }
}

Import UserFilter class to related controller.

<?php

use App\Filters\UserFilter;

class UserController extends Controller
{
    public function index(UserFilter $filter)
    {
        return User::filter($filter)->get();
    }
}

📜 License

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

About

Laravel Eloquent Filter is a package for filtering model data.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages