ayman-els/laravel-filter-package 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ayman-els/laravel-filter-package

最新稳定版本:v1.0.0

Composer 安装命令:

composer require ayman-els/laravel-filter-package

包简介

A Laravel package with a custom filtering trait

README 文档

README

Filter your queries based on url query string parameters like a breeze.

Compatible with Laravel 5.x 6.x 7.x 8.x 9.x 10.x.

Table of Content

Describing the Problem

You have probably faced the situation where you needed to filter your query based on given parameters in url query-string and after developing the logics, You've had such a code:

$users = User::latest();

if(request('username')) {
    $users->where('username', request('username'));
}

if(request('age')) {
    $users->where('age', '>', request('age'));
}

if(request('email')) {
    $users->where('email', request('email'));
}

return $users->get();

This works, But it's not a good practice.

When the number of parameters starts to grow, The number of these kind of if statements also grows and your code gets huge and hard to maintain.

Also it's against the Open/Closed principal of SOLID principles, Because when you have a new parameter, You need to get into your existing code and add a new logic (which may breaks the existing implementations).

So we have to design a way to make our filters logics separated from each other and apply them into the final query, which is the whole idea behind this package.

Usage

  1. First you need to install the package:

$ composer require ayman-els/laravel-filter-package

  1. Then you should use the Filter trait in your model, And define $filterCols, $filterDates, $filterSearchCols, $filterColsChilds property.
use AymanEls\LaravelFilterPackage\Traits\Filter;

class Product extends Model
{
    use Filter;

    public static $filterCols = ['category_id', 'department_id', 'user_id'];
    public static $filterDates = ['from' => 'created_at', 'to' => 'created_at'];
    public static $filterSearchCols = ['name_en', 'name_ar', 'description'];
    public static $filterColsChilds = ['currency' => 'department'];

    ...
}
  1. You need to use filter() method in your eloquent query. For example:
Product::filter()->get();

assume our query is something like this:

?search=field&from=2023-01-01&to=2023-01-02&category_id=1&department_id=1&user_id=1&currency=1

统计信息

  • 总下载量: 97
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-15