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
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
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-04