承接 ferfabricio/rest-get-filters 相关项目开发

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

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

ferfabricio/rest-get-filters

Composer 安装命令:

composer require ferfabricio/rest-get-filters

包简介

Filters for Laravel applications using Eloquent scopes

README 文档

README

Build Status Quality Gate Status Coverage Vulnerabilities

rest-get-filters

Filters for GET requests to be used in Laravel projects

Motivation

There are several approaches to create filters in GET requests, including this one more approach to the subject, but many of them generate a lot of work for the developer to do something simple.

The main objective of this package is to do something simplistic and that works well.

Instalation

composer require ferfabricio/rest-get-filters

Usage

Steps to setup this package:

  1. Add the Filterable trait;
  2. Define the filters;
  3. Apply the filters in you query;

Add the Filterable trait

As a example model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
    protected $fillable = [
        'name'
    ];
}

Add the trait FerFabricio\RestGetFilters\FilterFactory\Filterable.

<?php

namespace App;

use FerFabricio\RestGetFilters\FilterFactory\Filterable;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
    use Filterable;
  
    protected $fillable = [
        'name'
    ];
}

Define the filters

You can define filters adding a protected parameter $filters as an array with the column name in the keys and the value the type of the filter that you will apply.

Each Filter has a constant called IDENTIFIER that needs to be used into the $filters definition.

<?php

namespace App;

use FerFabricio\RestGetFilters\Traits\Filterable;
use FerFabricio\RestGetFilters\Filters\Date as DateFilter;
use FerFabricio\RestGetFilters\Filters\Like as LikeFilter;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
  use Filterable;
  
  protected $fillable = [
      'name'
  ];
  
  protected $filters = [
      'created_at' => DateFilter::IDENTIFIER,
      'name' => LikeFilter::IDENTIFIER
  ];
}

Apply the filters in you query

The Filterable trait add to the Model a scope that apply all filters configured on $filters variable, to apply the scope on your query you need to call the filters method;

<?php

namespace App\Http\Controllers;

use App\Example;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ExampleController extends Controller
{
    /**
     * List all Examples with filters
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request) : JsonResponse
    {
        // You need validate the input values
        $filters = $request->get('filters', []);
        $examples = Example::filters($filters)->get();
        return response()->json($examples);
    }
}

Available Filters

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固