n7olkachev/laravel-filterable 问题修复 & 功能扩展

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

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

n7olkachev/laravel-filterable

Composer 安装命令:

composer require n7olkachev/laravel-filterable

包简介

Nice and simple scope for your models

README 文档

README

Code quality Licence Build Status

Why?

This package is powered by standard Laravel scopes, instead of other similar packages, that brings something like Filter classes to your code, so it is much more easy to jump into. Also, if you decide to remove this package from your project, you will stay with standard scopes which can be used directly further.

Personally, I use this trait for faster development, combining it with $request->all()

Page::filter($request->all())->get()

By default, you get equality filters (where field = bar) and when you need to support other queries, adding new scopes will do the trick, without changing anything except model. See examples for better understanding.

Examples

class Page extends Model
{
    use Filterable;

    protected $fillable = [
        'title',
        'status',
        'created_at',
    ];
    
    protected $filterable = [
        'title',
        'created_after'
    ];

    public function scopeCreatedAfter($query, $time)
    {
        return $query->where('created_at', '>', $time);
    }
}

Now, we can use filter scope to filter our queries:

Page::filter(['title' => 'Cool page'])->first(); // equals to where('title', 'Cool page')

Page::filter(['status' => ['new', 'active'])->get() // equals to whereIn('status', ['new', 'active'])

Page::filter(['created_after' => '2017-01-01'])->get() // equals to createdAfter('2017-01-01') (notice our scope in Page class)

Of course it supports filters with multiple keys:

Page::filter(['title' => 'Cool page', 'status' => 'active'])->first()

Installation

You can install the package via composer:

composer require n7olkachev/laravel-filterable

Next, add Filterable trait and list all filterable properties:

use Filterable;

protected $filterable = ['created_at', 'title'];

That's all!

Testing

$ composer test

Credits

Sponsored by

https://websecret.by/

Web agency based in Minsk, Belarus

License

The MIT License (MIT)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固