Laravel 5.6 - 将附加参数传递给 API 资源? [英] Laravel 5.6 - Pass additional parameters to API Resource?

查看:27
本文介绍了Laravel 5.6 - 将附加参数传递给 API 资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel API 资源 可以是单个资源或集合.在某些情况下,需要将附加参数从控制器传递给资源/集合.下面是一个简单的示例,演示了使用 User 作为单个/集合资源以及传递给资源以进行输出的自定义 $apple 参数的问题.该问题可以在下面的最终 Output (Collection) 中看到,其中对于 fruit 值,我们得到了错误的 banana 值第一个用户,而不是正确的 apple 值(所有其他用户都得到).它非常适用于单个输出,而不是集合.见下文:

A Laravel API Resource can be either a single resource or a collection. In some cases, additional parameters are required to be passed to the resource/collection from the controller. Below is a simple example demonstrating the issue using User as a single/collection resource, and a custom $apple parameter to be passed to the resource for output. The issue can be seen in the final Output (Collection) below, where for the fruit value, we get an incorrect value of banana for the first user, instead of the correct apple value (which all other users get). It works perfectly for the single output, just not the collection. See below:

带有 UserResource 的控制器(单个)

    $user = User::first();
    return new UserResource($user, $apple = true); // $apple param passed

带有 UserResource(集合)的控制器

    $users = User::limit(3)->get();
    return UserResource::collection($users, $apple = true); // $apple param passed

用户资源

    <?php

    namespace AppHttpResources;
    use IlluminateHttpResourcesJsonJsonResource;

    class UserResource extends JsonResource {
        private $apple;

        public function __construct($resource, $apple = false) {
            // Ensure we call the parent constructor
            parent::__construct($resource);
            $this->resource = $resource;
            $this->apple = $apple; // $apple param passed
        }

        public function toArray($request) {
            return [
                'id'     => (int) $this->id, 
                'name'   => $this->name,
                'fruit'  => $this->apple ? 'apple' : 'banana',
            ];
        }
    }

输出(单个)

    {
        "data": {
            "id": 1,
            "name": "Peter",
            "fruit": "apple" // correct param!
        }
    }

输出(集合)

    {
        "data": [
            {
                "id": 1,
                "name": "Peter",
                "fruit": "banana" // INCORRECT param!
            },
            {
                "id": 2,
                "name": "Lois",
                "fruit": "apple" // correct param!
            },
            {
                "id": 3,
                "name": "Brian",
                "fruit": "apple" // correct param!
            }
        ]
    }

请注意,这只是一个例子,它可以是任意数量的随机参数(与User集合无关,但必须为输出逻辑传递),例如单个值read_at 来自不同表的时间戳我想传递一次,并在输出之前在资源集合中对其执行一些逻辑(例如与用户时间戳的比较),或为附加逻辑传递的其他参数 if/else 通常在资源文件中执行以操作集合的输出.这怎么办?

Please note that this is just an example, it can be any amount of random parameters (unrelated to the User collection, but must be passed for output logic), such as a single value read_at timestamp from a different table I want to pass once, and do some logic on it in the resource collection before output (like comparison to a user timestamp), or other parameters passed for additional logic if/else to be performed in the resource file in general to manipulate output of collection. How can this be done?

推荐答案

以下方法对我有用:

用户资源

class UserResource extends Resource{

    protected $foo;

    public function foo($value){
        $this->foo = $value;
        return $this;
    }

    public function toArray($request){
        return [
            'id' => $this->id,
            'name' => $this->name,
            'foo' => $this->foo,
         ];
    }

    public static function collection($resource){
        return new UserResourceCollection($resource);
    }
}

用户集合

class UserResourceCollection extends ResourceCollection{

    protected $foo;

    public function foo($value){
        $this->foo = $value;
        return $this;
    }

    public function toArray($request){
        return $this->collection->map(function(UserResource $resource) use($request){
            return $resource->foo($this->foo)->toArray($request);
    })->all();

        // or use HigherOrderCollectionProxy
        // return $this->collection->each->foo($this->foo)->map->toArray($request)->all()

        // or simple
        // $this->collection->each->foo($this->foo);
        // return parent::toArray($request);
    }
}

传递附加参数的不同方式

(new UserResource($user))->foo('bar');
(new UserResourceCollection($user))->foo('bar');

UserResource::make($user)->foo('bar');
UserResourceCollection::make($users)->foo('bar');
UserResource::collection($users)->foo('bar');

这篇关于Laravel 5.6 - 将附加参数传递给 API 资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆