定制 ehsan-coder/resource-methods 二次开发

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

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

ehsan-coder/resource-methods

最新稳定版本:v1.0.0

Composer 安装命令:

composer require ehsan-coder/resource-methods

包简介

this package added new methods to modified your api resource, such as you can use only or except methods to customize your api resources when use in controllers.

README 文档

README

Logo

Eloquent API Resource Methods

API Resources in laravel is very common, If you are here, you most likely know about and use them. API Resources is very useful, but it lacks a bit of the "Laravel way" when you want use methods such as except or only, because you may want to modified output of the Resource. by this package you can do this!

Installation

You can install the package via composer:

 composer require ehsan-coder/resource-methods

Usage/Examples

On any API Resource you want to be able to use the methods described below, you should use the following trait:

use EhsanCoder\ResourceMethods;

class UserResource extends JsonResource
{
    use ResourceMethodsTrait;
}

This package provides a few features.

Only method

if your resource have a toArrray method like this, you can use only method such as below:

    public function toArray($request)
    {
        $response = [
            'id'              => $this->id,
            'username'        => $this->username,
            'status'          => $this->status,
            'name'            => $this->name,
        ];
        return $response;
    }
    $user                 = \App\Models\User::query()->first();
    $userThatHaveOnlyName = \App\Http\Resources\UserResource::make($user)->only('name');

in this method, if you want use multi property for apply only method you can use such as below:

    $user                      = \App\Models\User::query()->first();
    $userThatHaveOnlyNameAndId = \App\Http\Resources\UserResource::make($user)->only(['id','name']);

Except method

if your resource have a toArrray method like this, you can use except method such as below:

    public function toArray($request)
    {
        $response = [
            'id'              => $this->id,
            'username'        => $this->username,
            'status'          => $this->status,
            'name'            => $this->name,
        ];
        return $response;
    }
    $user                 = \App\Models\User::query()->first();
    $userThatHaveAllPropertiesExceptName = \App\Http\Resources\UserResource::make($user)->except('name');

in this method, if you want use multi property for apply except method you can use such as below:

    $user                      = \App\Models\User::query()->first();
    $userThatHaveAllPropertiesExceptNameAndId = \App\Http\Resources\UserResource::make($user)->except(['id','name']);

in except method, you can do a magical work! if you have a nested json, In other words you have a property with value json you can except any key-value into this json value, for examaple:

    $device                      = \App\Models\Devices::query()->first();
    $deviceThatExceptSomeKeyValuesInModelJsonProperty = \App\Http\Resources\DeviceResource::make($device)->except(['model->name', 'model->brand->country'])

overwrite method

in this method you can modified any value of any output resource propery.for example:

    $user                          = \App\Models\User::query()->first();
    $camelCaseNameValueForThisUser = \App\Http\Resources\UserResource::make($user)->overwrite('name', fn(string $name) => camel_case($name));

Feedback

If you have any feedback, please reach out to us at msc.shafiei@gmail.com

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固