承接 cyd622/laravel-filterable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

cyd622/laravel-filterable

Composer 安装命令:

composer require cyd622/laravel-filterable

包简介

Laravel package for dynamically filtering Eloquent results using URL query string.

README 文档

README

This package gives you a convenient way to automatically filter Eloquent results based on query string parameters in the URL. Filterable parses the query string, compares it with columns that you'd like to automatically filter, then creates a dynamic scope that is used by Eloquent to construct the SQL.

Installation

Add the package to 'require' in your composer.json file:

"require": {
    "cyd622/laravel-filterable": "dev-master"
},

Run 'composer dump-autoload' from the command line:

#composer dump-autoload

Register the service provider in 'app/config/app.php'. Service provider:

'providers' => array(
    \\...
    'Laravel\Filterable\FilterableServiceProvider',
    \\...
);

License

Copyright 2014 Dave Hodgins Released under MIT license (http://opensource.org/licenses/MIT). See LICENSE file for details. Usage

NOTE: this package also includes a version (FilterableWrapper.php) that can be used to wrap a DB or Eloquent object, and a version (FilterableTrait.php) that can be used as a trait with an Eloquent model.

Filterable.php

Edit your Eloquent model to extend 'Laravel\Filterable\Filterable'.

class Object extends Laravel\Filterable\Filterable {
    // ...
}

FilterableWrapper.php

Give FilterableWrapper a DB or Eloquent object.

$object = DB::table('objects');
$objects = FilterableWrapper($object);

FilterableTrait.php

class Object extends Eloquent {

   use Laravel\Filterable\FilterableTrait;

}

The examples below use the Filterable class!

In the above example, class Object corresponds to table 'objects':

id color shape total
1 red square 150
2 blue square 2000
3 green circle 575
4 yellow triangle 15
5 red triangle 900
6 red triangle 600

Filterable Columns

Specify the column you want to automatically filter.

$columns = [ 'color', 'shape', 'total' ];

For example:

 http://www.your-site/?color=blue&shape=round&total=500

You can also alias the columns if you prefer not to reveal them:

$columns = [ 'col' => 'color', 'sha' => 'shape', 'tot' => 'total' ];

For example:

http://www.your-site/?col=blue&sha=round&tot=500

To filter results, simply pass the columns to Eloquent using filterColumns():

$objects = Object::filterColumns($columns)->get()->toArray();

You can also filter joins:

$columns = array('color' => 'objects.color',
                 'name' => 'objects.name',
                 'shape' => 'objects.shape',
                 'category' => 'cat_object.cat_id');
$objects = Object::join('cat_object', 'objects.id', '=', 'cat_object.object_id')
                   ->filterColumns($columns)
                   ->get()->toArray();

And you can filter eager loads:

/**
 * Columns available in main query
 */
$columns = array('color' => 'objects.color',
                 'name' => 'objects.name',
                 'shape' => 'objects.shape');
$objects = Object::with(array('categories' => function($q) {
               /**
                * Columns available to sub-query
                */
               $columns = array('category' => 'cat_object.cat_id');
               $q->filterColumns($columns);
           }))->filterColumns($columns)
           ->get()
           ->toArray();

The following examples demonstrate how query string parameters can be used. Single Value

?color=red

SELECT ... WHERE ... color = 'red'
id color shape total
1 red square 150
5 red triangle 900
6 red triangle 600
Multiple Values
?color[]=red&color[]=blue

SELECT ... WHERE ... color = 'red' OR color = 'blue'
id color shape total
1 red square 150
2 blue square 2000
5 red triangle 900
6 red triangle 600
Multiple Parameters
?color[]=red&shape[]=triangle

SELECT ... WHERE ... color = 'red' AND shape = 'triangle'
id color shape total
5 red triangle 900
6 red triangle 600
Boolean Operators
?color[]=red&shape[]=triangle&bool[shape]=or

SELECT ... WHERE ... color = 'red' OR shape = 'triangle'
id color shape total
4 yellow triangle 15
5 red triangle 900
6 red triangle 600
Comparison Operators

Greater Than

?total=599&operator[total]=>

SELECT ... WHERE ... total > '599'
id color shape total
2 blue square 2000
5 red triangle 900
6 red triangle 600

Less Than

?total=600&operator[total]=<

SELECT ... WHERE ... total < '600'
id color shape total
1 red square 150
3 green circle 575
4 yellow triangle 15

Not Equal

?shape=triangle&operator[shape]=!=

SELECT ... WHERE ... shape != 'triangle'
id color shape total
4 yellow triangle 15
5 red triangle 900
6 red triangle 600

Between

?total[start]=900&total[end]=5000

SELECT ... WHERE ... total BETWEEN '900' AND '5000'
id color shape total
2 blue square 2000
5 red triangle 900

cyd622/laravel-filterable 适用场景与选型建议

cyd622/laravel-filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「laravel」 「eloquent」 「Laravel 4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cyd622/laravel-filterable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cyd622/laravel-filterable 我们能提供哪些服务?
定制开发 / 二次开发

基于 cyd622/laravel-filterable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-31