mrjmpl3/laravel-restful-helper

该软件包已被废弃,不再维护。未建议替代包。

Laravel Helper用于Restful项目

4.0.0-alpha.7 2020-07-21 04:23 UTC

README

安装

通过Composer

$ composer require mrjmpl3/laravel-restful-helper

用法

此软件包使查询依赖于请求,类似于GraphQL。

请求

  • 过滤数据: /product?column=value&column2=value2
  • 排序数据: /product?sort=-column1,column2
    • 带有负前缀 = 降序
    • 没有负前缀 = 升序
  • 选择字段数据: /product?fields=column1,column2,column3,column4
  • 分页和每页: /product?paginate=true&per_page=5
  • 嵌入: /product?embed=relationfunction

代码

转换为集合

// Create a simple instance of model where you want apply the queries
$model = new Product();
$responseHelper = new ApiRestHelper($model);
          
// The method 'toCollection' return a collection with all data filtered
$response = $responseHelper->toCollection();

转换为模型

// Create a simple instance of model where you want apply the queries
$model = new Product();           
$responseHelper = new ApiRestHelper($model);
                
// The method 'toModel' return a model with all data filtered
$response = $responseHelper->toModel();

从构建器到集合

// Important! Don't close the query with get() or paginate()
$query = Product::where('state', = , 1);
$responseHelper = new ApiRestHelper($query);
          
// The method 'toCollection' return a collection with all data filtered
$response = $responseHelper->toCollection();

关系

  • 在模型中添加如下示例中的数组

    public $apiAcceptRelations = [
        'post'
    ];
    

    其中 'post' 是关系的函数名

  • 在API资源中使用embed函数

    public function toArray($request) {
        $embed = (new ApiRestHelper)->getEmbed();
                
        return [
          'id' => $this->id,
          'name' => $this->name,
          $this->mergeWhen(array_key_exists('post', $embed), [
              'post' => $this->getPostResource($embed),
          ]),
          'created_at' => $this->created_at,
          'updated_at' => $this->updated_at,
        ];
    }
            
    private function getPostResource($embedRequest) {
      $postResource = NULL;
                
      if (array_key_exists('local', $embed)) {
          $postRelation = $this->local();                    
          $fieldsFromEmbed = (new ApiRestHelper($postRelation->getModel()))->getEmbedField('post');
                    
          if(!empty($fieldsFromEmbed)) {
              $postResource = new PostResource($postRelation->select($fieldsFromEmbed)->first());
          } else {
              $postResource = new PostResource($postRelation->first());
          }
      }
            
      return $postResource;
    }
    

转换器

  • 在模型中添加如下示例中的数组
public $apiTransforms = [
    'id' => 'code'
];

其中 'id' 是数据库列名,'code' 是响应中重命名的列

  • 在API资源中使用$apiTransforms数组
$apiHelper = new ApiRestHelper($this);

return [
    $apiHelper->getKeyTransformed('id') => $this->id,
    'name' => $this->name,
    'created_at' => $this->created_at,
    'updated_at' => $this->updated_at,
];
  • 要使用API资源中的字段,可以结合转换器字段
$apiHelper = new ApiRestHelper($this);

return [
    $this->mergeWhen($apiHelper->existInFields('id') && !is_null($this->id), [
        $this->transforms['id'] => $this->id
    ]),
    $this->mergeWhen($apiHelper->existInFields('name') && !is_null($this->name), [
        'name' => $this->name
    ]),
]

过滤中排除字段

  • 在模型中添加如下示例中的数组
public $apiExcludeFilter = [
    'id'
];

其中 'id' 是要排除的数据库列名

变更日志

请参阅CHANGELOG以获取最近更改的更多信息。

安全性

如果您发现任何安全相关的问题,请发送电子邮件至jmpl3.soporte@gmail.com,而不是使用问题跟踪器。

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。