andrewdevelop/eloquent-filter 问题修复 & 功能扩展

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

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

andrewdevelop/eloquent-filter

最新稳定版本:2.0.0

Composer 安装命令:

composer require andrewdevelop/eloquent-filter

包简介

A basic filtering package for Laravel's query builder

README 文档

README

Installation

composer require andrewdevelop/eloquent-filter

Usage example

Some command handler, controller, etc.

<?php

use App\Task;
use App\TaskFilter;
use Illuminate\Http\Request;

class SomeHandlerClass
{
    /** 
     * An instance of Illuminate\Database\Eloquent\Model.
     * @var Task 
     */
    private $tasks;
    
    /**
     * The instance of the custom filter (see bellow).
     * @var TaskFilter
     */
    private $filter; 

    /**
     * Some constructor with dependencies for example.
     * @param Task $tasks
     * @param TaskFilter $filter
     */
    public function __construct(Task $tasks, TaskFilter $filter)
    {
        $this->tasks = $tasks;
        $this->filter = $filter;
    }
    
    /** Some place for the business logic */
    public function handle(Request $request) 
    {
        /** @var Illuminate\Database\Eloquent\Builder */
        $query = $this->tasks->query();
        return $this->filter
            // Put the builder here...
            ->send($query) 
            // Put the request or an array here...
            ->through($request) 
            // Also can use some variables...
            ->with('role', 'admin')
            // Run apply() to do the magic!..
            ->apply()
            // paginate(), get(), or query().
            ->paginate();
    }
}

The custom filter's code:

<?php

class TaskFilter extends QueryFilter
{
    /**
     * Filters that always applied. Contains key and default value.
     * @var array
     */
    protected $always_apply = [
        'order_by' => 'created_at',
    ];

    /**
     * Filter "order_by" & "dir"
     * @return void
     */
    protected function applyOrderBy()
    {
        $order_by = $this->input->getOrdering();
        $direction = $this->input->getDirection();
        $this->query->orderBy($order_by, $direction);
    }

    /**
     * Follow the naming convention.
     * "snake_case" params will be converted to "apply"+"PascalCase". 
     * E.g. "state" query param will be call the applyState method.
     * @param null|array|string $choices
     */
    protected function applyState($choices = null)
    {
        if (!is_array($choices)) $choices = [$choices];
        $choices = array_filter($choices, function($e) {
            return in_array($e, TaskStates::values());
        });
        if (count($choices)) $this->query->whereIn('state', $choices);
    }
    
    /** Und so weiter... */
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固