alexskoromnui/laravel-filters 问题修复 & 功能扩展

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

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

alexskoromnui/laravel-filters

Composer 安装命令:

composer require alexskoromnui/laravel-filters

包简介

Dynamic create query with filters for search

README 文档

README

Package for dynamic adding new search filters.

Install package

composer require alexskoromnui/laravel-filters

Usage

For demonstration i will use ExampleModel class which locate in app folder, filters for this model will locate in Filters folder.

Step 1

Your search model should implement SearchableModel interface from this package which has one method getFiltersNamespace() in which you should specify namespace of filters for this model.


namespace App;

use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model implements SearchableModel
{
	    public function getFiltersNamespace(): string
    {
        return 'App\Filters';
    }
}

Step 2

Let's imagine that ExampleModel has name property, and in request we have ?name=TEST

Step 3

You should create filter and implement Filter interface from this package which has one method apply(Builder $builder, $value) in which you should write your query.

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;
use Skoromnui\Filters\Filter;

class Name implements Filter
{
    public function apply(Builder $builder, $value)
    {
        return $builder->where('name', 'like', '%' . $value . '%');
    }
}

Step 4

After all this steps your Controller or Service will look like this:

use App\Http\Controllers\Controller;
use App\ExampleModel;
use Illuminate\Http\Request;
use Skoromnui\Filters\Service\SearchService;

class ExampleController extends Controller
{
    public function search(Request $request, SearchService $searchService)
    {
        $query = $searchService->buildQuery($request, new ExampleModel());
        
        $result = $query->get();

        return response()->json($result);
    }
}

Step 5

SearchService from this package is engaged in build query, after that you can use any methods like (get(), paginate(), limit(), offset(), count() ..)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固