定制 wstam/eloquentfilter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

wstam/eloquentfilter

Composer 安装命令:

composer require wstam/eloquentfilter

包简介

This package implements easy model filtering into your Laravel models and generates HTML filters in your template

README 文档

README

EloquentFilter adds new functionalities to your Laravel Eloquent model to make filtering data easy.

Installing EloquentFilter

You need to use Composer to install EloquentFilter into your project:

composer require wstam/eloquentfilter

Configuring (Laravel)

Now you have to include EloquentFilterServiceProvider in your config/app.php:

'providers' => [
    /*
     * Package Service Providers...
     */
    WStam\EloquentFilter\EloquentFilterServiceProvider::class,
]

Now we need to publish the default blade views by running the following Artisan command in your terminal:

php artisan vendor:publish --provider="WStam\EloquentFilter\EloquentFilterServiceProvider"

Now you have container.blade.php and filter.blade.php in your vendor/eloquentfilter view folder. You can change the the template of the filter container and the filter itself to make it fit to your own look and feel.

How to use EloquentFilter

For an example, you want to add filtering to the Product model. Add the filtering trait to your model like this example:

<?php

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use WStam\EloquentFilter\EloquentFilterable;
 
class Product extends Model
{
    use EloquentFilterable;
}

Now you can specify filters to your Query Builder object:

/**
 * Show all the products
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $products = Product::query()->orderBy('title', 'ASC');
 
    // addFilter($column, $label = null, $type = 'text', $default = null, $comparison = '=')
 
    $products->addFilter('title', 'Title', 'text');
    $products->addFilter('external_supplier', 'External supplier', 'boolean');
    
    // Use filterCollection instead of get() to implement the "renderFilter" method into the collection
    $productCollection = $products->filterCollection();
 
    return view('product.index', ['products' => $productCollection]);
}

Now you can render the filters inside your blade template:

<!-- This renders the filters. You can change the filter views at vendor/eloquentfilter -->
{!! $products->renderFilters() !!}
 
<div class="table-responsive">
    <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>Title</th>
                <th>External supplier</th>
            </tr>
        </thead>
        <tbody>
        @foreach($products as $product)
            <tr>
                <td>
                    <a href="{{ route('product_edit', $product->id) }}">{{$product->title}}</a>
                </td>
                <td>
                    @if($product->external_supplier == 1)
                        Yes
                    @else
                        No
                    @endif
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>

Contributing

Do have good ideas about expanding/enhancing this library? Feel free to contribute and send a pull request!

License

EloquentFilter is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固