mhamzeh/presenter-filter 问题修复 & 功能扩展

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

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

mhamzeh/presenter-filter

Composer 安装命令:

composer require mhamzeh/presenter-filter

包简介

README 文档

README

Introduction

The package allows you to create two types of classes: filter class and presenter class

Filter

This package allows you to easily handle database filtering through query strings.

example: /users?status=1&name='kami'

Presenter

Sometimes our models get too fat, and that can make our development a lot harder. In this case, we use a second class that has the same function as the model and is used as a second model, and the Harrow method can be included in this class.

Installation

you can install the package via composer:

composer require mhamzeh/presenter-filter

You Must by publishing configuration by issuing following artisan command php artisan vendor:publish.

Usage

You have access to two commands and you can use them to create your own filter and presenter classes

make:filter command

You can use the following command to create a new filter.

php artisan make:filter UserFilter

This will create a new filter in the app/Filters directory.

options:

1-You can add the model to the command

‍‍‍‍‍‍php artisan make:filter UserFilter --model=User

Used by default Models folder If you have saved models elsewhere, change the config Modules of this folder

make:presenter Command

You can use the following command to create a new Presenter

php artisan make:presenter UserPresenter

options:

1- You can add the model to command

php artisan make:presenter UserPresenter --model=User

Example With Filter

Let's say you want to use filterable on User model. You will have to create the filter class App/Filters/PostFilter.php (php artisan make:filter PostFilter --model=Post)

If you use the --model option, filterable will be added directly to the model

<?php
namespace App\Filters;

use mhamzeh\packageFp\Filters\contracts\QueryFilter;

class UserFilter extends QueryFilter
{
    public function name($value){
        return $this->builder->where('name','LIKE',"%$value%");        
    }
}

Now you need to add local scope to your model if you have not used the --model option:

use mhamzeh\packageFp\Filters\contracts\Filterable;
...
class User extends Model
{
    use Filterable;
    ...
}

Finaly, call the scope in controller like so:

use App\Filters\UserFilter;
...
public function index()
{
    return User::filters(new UserFilter())->paginate();
}

Example With Presenter

Let's say you want to use Presentable And introduce the presenter class on User model. You will have to create the filter class App/Presenter/UserPresenter.php (php artisan make:presenter UserPresenter --model=User)

If you use the --model option, Presentable and presenter class will be added directly to the model

<?php
namespace App\Presenter;

use mhamzeh\packageFp\Presenter\Contracts\Presenter;

class UserPresenter extends Presenter
{
    public function full_name(){
         return $this->entity->name ." ".$this->entity->full_name;        
    }
}

Now you need to add local scope to your model if you have not used the --model option:

use mhamzeh\packageFp\Presenter\Contracts\Presentable;
...
class User extends Model
{
    use Presentable;
    protected $presenter = UserPresenter::class;

    ...
}

Finally you can use this method in your Blade or Api Resources or Controller for example Blade:

 @foreach($users as $user)
     <div>
          <p>fullname : $user->present()->full_name </p>
      </div>
 @endforeach

in Api Resources:

class UserResource extends JsonResource
{
   /**
    * Transform the resource into an array.
    *
    * @param \Illuminate\Http\Request $request
    * @return array
    */
   public function toArray($request)
   {
       return [
            'id'=>$this->id,
            'full_name'=> $this->present()->full_name,
            'email'=>$this->email
        ];
   }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固