administrcms/query-filters 问题修复 & 功能扩展

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

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

administrcms/query-filters

Composer 安装命令:

composer require administrcms/query-filters

包简介

Query filters for an Eloquent model

README 文档

README

Inspired by Laracasts and Jeffrey Way - https://github.com/laracasts/Dedicated-Query-String-Filtering/

Install using Composer

composer require administrcms/query-filters

And add the ServiceProvider - Administr\QueryFilters\QueryFiltersServiceProvider::class

Usage

Define a QueryFilter class (you can run the php artisan administr:query-filter filterName command to generate the scaffold):

<?php

namespace App\QueryFilters;

use Illuminate\Database\Eloquent\Builder;
use Administr\QueryFilters;

class LessonFilters extends Filter 
{
    /**
     * @param  string $order
     * @return Builder
     */
    public function popular($order = 'desc')
    {
        return $this->builder->orderBy('views', $order);
    }

    /**
     * @param  string $level
     * @return Builder
     */
    public function difficulty($level)
    {
        return $this->builder->where('difficulty', $level);
    }
}

Make your model use the Administr\QueryFilters\Filterable trait.

And lastly in your controller or where you want this logic to run:

<?php

namespace App\Http\Controllers;

use App\Models\Lesson;
use App\QueryFilters\LessonFilters;

class LessonsController extends Controller
{
    public function index(LessonFilters $filters)
    {
        return Lesson::filter($filters)->get();
    }
}

ListView sorting

By default the base Filter class will handle the sorting for your main table. If you have a ListView that uses a column which is a relationship to the model, you'll have to handle that column for yourself. Let's imagine that you have User model and it has a relationship to UserType (one User has one UserType). So in your ListView you want to sort by the column that shows the name of the UserType. We'll assume that the column in the ListView is defined as type.name. In that case you need to define the custom sort like this:

public function sortTypeName($dir)
{
    return $this
        ->builder
        ->join('user_types', 'user_types.id', '=', 'users.type_id')
        ->orderBy('user_types.id', $dir);
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-10-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固