定制 mayoz/eloquent-filterable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

mayoz/eloquent-filterable

Composer 安装命令:

composer require mayoz/eloquent-filterable

包简介

Query filter for your Eloquent models.

README 文档

README

With this package, you can optimize query clauses calling before or after the filter. You can manage your queries from a single interface.

Installation

PHP 5.5.9+ or HHVM, and Composer are required.

To get the latest version of Eloquent Filterable, simply add the following line to the require block of your composer.json file:

"require": {
    "mayoz/eloquent-filterable": "~2.0"
}

You'll then need to run composer install or composer update to download it and have the autoloader updated. Or use to shortcut installed through terminal:

composer require mayoz/eloquent-filterable ~2.0

Usage

  1. Create your query filters.
  2. Create your Eloquent models.
  3. Define Filterable trait and use the query filters in the Eloquent model.

Create Filters

All filters should be extend Mayoz\Filter\Filter abstract class. Thus, can be used before and after methods in your filters.

The Before Method

The before method responsible to position the head of the WHERE clause of the query. For example; we need published = 1 of query WHERE clause. However, this clause would like to work on before the other clauses.

<?php namespace App\Filters;

use Mayoz\Filter\Filter;

class PublishedFilter extends Filter
{
	/**
	 * The first executable filter clause.
	 *
	 * @param  mixed  $query
	 * @return void
	 */
	public function before($query)
	{
		$query->where('published', '=', 1);
	}
}

The After Method

The after method responsible to position the end of the WHERE clause of the query. For example, if need status = 'active' of query WHERE clause. However, this clause would like to work on after the other clauses.

<?php namespace App\Filters;

use Mayoz\Filter\Filter;

class StatusActiveFilter extends Filter
{
	/**
	 * The last executable filter clause.
	 *
	 * @param  mixed  $query
	 * @return void
	 */
	public function after($query)
	{
		$query->where('status', '=', 'active');
	}
}

Create Model

Create your model file. If you want to manage queries add the Filterable trait your model file. And than, assign the all associative filters to $filters variable. Consider the following example:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Mayoz\Filter\Filterable;

class Post extends Model
{
    use Filterable;

	/**
	 * The attributes that should be filter.
	 *
	 * @var array
	 */
	protected $filters = [
		'App\Filters\StatusActiveFilter',
		'App\Filters\PublishedFilter'
	];

	// other things...
}

Important: The order of the filter is important when adding (if need multiple before or after filters) query clause. The before filters are added the head of the clause according to the reference sequence. Likewise, the after filters.

Debug

Now the Post model is ready to use. You ready? Okay, we're testing the query. Don't forget to enable the query logging for examine the model queries.

$model = \App\Post::where('views', '>', 100)->get();

Query logging output:

SELECT *
FROM   `posts`
WHERE  `published` = 1     # added by automatic filter.
  AND  `views` > 100       # user defined where clause
  AND  `status` = 'active' # added by automatic filter.

Why Use?

For example, you might want to show only the approved text of the visitors on the site. However, administrators can see them all. Create a new extended filter:

<?php namespace App\Filters;

use Auth;
use Mayoz\Filter\Filter;

class StatusActiveFilter extends Filter
{
	/**
	 * The last executable filter clause.
	 *
	 * @param  mixed  $query
	 * @return void
	 */
	public function after($query)
	{
		# assume, the `users` table has `role` field.
		if (array_get(Auth::user(), 'role') != 'administrator')
		{
			$query->where('status', '=', 'active');
		}
	}
}

Hocus, pocus! Visitors will display only the active posts. But administrator display all. Ok?

Contributing

Love innovation and simplicity. Please use issues all errors or suggestions reporting. Follow the steps for contributing:

  1. Fork the repo.
  2. Follow the Laravel Coding Style.
  3. If necessary, check your changes with unittest.
  4. Commit all changes.
  5. Push your commit and create a new PR.
  6. Wait for merge the PR.

Unit Test

Please create your tests and check before PR. Use the command:

$ phpunit

License

Eloquent Filterable is licensed under The MIT License (MIT).

mayoz/eloquent-filterable 适用场景与选型建议

mayoz/eloquent-filterable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 117 次下载、GitHub Stars 达 9, 最近一次更新时间为 2015 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-28