manajet/laravel-extra-resource 问题修复 & 功能扩展

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

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

manajet/laravel-extra-resource

最新稳定版本:v2.0.0

Composer 安装命令:

composer require manajet/laravel-extra-resource

包简介

This packages allows you to add extra parameters to your Eloquent API resources and collections.

README 文档

README

![Software License][ico-license] [![Total Downloads][ico-downloads]][link-downloads]

About

This packages allows you to add extra parameters to your Eloquent API resources and collections.

Highly inspired by gdebrauwer's issue answer and his pull-request.

Installation

Require the manajet/laravel-extra-resource package in your composer.json and update your dependencies:

composer require manajet/laravel-extra-resource

Create resource

You can create a new resource with this command:

php artisan make:extraresource MyResource

As well as Laravel make:resource command, you can pass --collection option to create a new collection with extra parameters:

php artisan make:extraresource MyCollection --collection

Usage

You can use your resources as you did before. To pass extra parameters, use the using method.

Resource:

$user = User::find(1);
return (new UserResource($user))->using(['foo' => 'bar']);

And in your Resource class:

<?php

namespace App\Http\Resources;

use Manajet\ExtraResource\ExtraJsonResource;

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

Collection:

Collection will try to use your Resource class, so you need to create it first.

$users = User::all();
return (new UserCollection($users))->using(['foo' => 'bar']);

You can also use it with the static collection method:

$users = User::all();
return UserResource::collection($users)->using(['foo' => 'bar']);

License

Released under the MIT License, see LICENSE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-11