【问题标题】:How to process Laravel Resources inside Another resource?如何在另一个资源中处理 Laravel 资源?
【发布时间】:2019-10-02 21:24:46
【问题描述】:

我在资源中有资源,通常它可以正常工作,但如果资源 1.1 中的资源 1.1.1 提供具有特定值的字段,我需要它在资源 1 处返回空数组。为了它不会显示集合 Resource 1 中的特定 Resource 1.1

尝试使用 json_decode 并强制资源处理并给出资源结果的字符串,然后 json_decode 将其返回到数组。

//资源1.1->

    return [
        'id' => (int)$this->id,
        'block' => BlockResource::collection($this->blocks)->data($this->parameters);
    ];

//(1.1.1) 块资源

 return [
        'id' => (int)$this->id,
        'groups' => Resource::collection()
    ];

dd($BlockResource); //显示资源对象而不是处理结果

$BlockResource = json_encode($BlockResource);

dd($BlockResource); //经过处理的字符串,可以做json_decode以数组形式返回......

例子:

resource1: [
                {
                    id : 1
                    block:{
                      id : 1
                      groups : [1,2,3,4,5]
                    }
                },
                {
                    id : 2
                    block:{
                      id : 2
                      groups : []
                    }
                }
]

2º resource1.1 不应显示,因为 groups 是一个空数组 我可以使用 json_encode/decode 技巧,但它会给我过多的处理。

有什么想法吗:D?

【问题讨论】:

    标签: laravel api resources lumen


    【解决方案1】:

    决定进入 JsonResource 方法和

    /**
     * Resolve the resource to an array.
     *
     * @param  \Illuminate\Http\Request|null  $request
     * @return array
     */
    public function resolve($request = null)
    

    为我工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-27
      • 2012-08-28
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      相关资源
      最近更新 更多