【问题标题】:Laravel Resource collection showing null fieldLaravel 资源集合显示空字段
【发布时间】:2021-11-03 22:14:41
【问题描述】:

我正在使用 Laravel 开发一个 API。在我正在访问的一个端点中,一些字段显示为空值,但它应该包含一些信息。

注意“addicionais_descricao”和“valor”字段,当我将它们包含在attributeitems数组中时,它们总是带有空值,但是如果我将其保留在初始级别,则会显示数据,但不能解决我的情况,因为我需要带有属性项的这些信息:

enter image description here

这是端点调用的地方,我在“Attribute”表中进行查询,该表与“Attributeitems”表有关系,而“attributeitems”表与“Attribute”和“product”链接。

public function show($id)
{

    $atributos = Atributo::query('atributo')
        ->select(
            'atributo.id',
            'atributo.atrdescricao',
            'atributoitens.atributo_id',
            'atributoitens.produto_id',
            'produto.prodescricao',
            'produto.provalor'
        )

        ->leftJoin('atributoitens', 'atributo.id', '=', 'atributoitens.atributo_id')
        ->leftJoin('produto', 'produto.id', '=', 'atributoitens.produto_id')
        ->where('atributo.id', '=', $id)

        ->get()->unique('id');

    return AtributoResource::collection($atributos);
}

资源属性:

    public function toArray($request)
{
    
    return [            
        'id' => $this->id,
        'descricao' => $this->atrdescricao, 
         'atributoitens' => AtributoitensResource::collection($this->atributoitens),
    ];
}

资源属性:

public function toArray($request)
{        
    
    return [
        'id' => $this->id,
        'atributo' => $this->atributo_id,
        'produtos' => $this->produto_id,            
        'adicionais_descricao' => $this->prodescricao,
        'valor' => $this->provalor            
        
    ];
}

这种情况的正确程序是什么?

【问题讨论】:

    标签: laravel api eloquent collections resources


    【解决方案1】:

    以这个例子作为参考:

    控制器

    $data = $shop->products()
            ->whereStatus(true)
            ->where('product_shop.active', true)
            ->where('product_shop.quantity', '>=', $this->min_product_qty)
            ->paginate(50);
    
    return (new ProductCollection($data))
        ->response()
        ->setStatusCode(200);
    

    产品集合

    public function toArray($request)
        {
            return [
                'data' => $this->collection
                          ->map(function($product) use ($request) {
                                return (new ProductResource($product))->toArray($request);
                            }),
                'brand' => $this->when($request->brand, $request->brand)
            ];
        }
    

    产品资源

        public function toArray($request)
        {
            return [
                'type' => 'product',
                'id' => (string) $this->id,
    
                'attributes' => [
                    'uuid' => $this->uuid,
                    'name' => $this->name,
                    'slug' => $this->slug,
                    'description' => $this->description,
                    'thumb_path' => $this->thumb_path,
                    'cover_path' => $this->cover_path,
                ],
    
                'relationships' => [
                    'brand' => $this->brand 
                ]
            ];
        }
    

    【讨论】:

    • 我尝试调整您的建议,但没有成功。我会告诉你我的结果。
    【解决方案2】:

    这样的事情应该可以帮助你做你想做的事。我不能完全为你做。顺便说一下,为什么你不使用 Eloquent,比如 Attribute::where(...)->with(['relation_1', 'products'])->get();

    public function toArray($request)
        {
            return [
                'id' => $this->id,
                'attributes' => [...],
                'products' => $this->collection
                          ->map(function($this->product) use ($request) {
                                return (new ProductResource($product))->toArray($request);
                            }),
            ];
        }
    

    【讨论】:

    • 我不使用它是因为缺乏知识。但我尝试使用“belongsToMany”(laravel.com/docs/8.x/…)关系。当我不使用 AttributeResource::collection 时结果很好,但是当我使用某些字段时为空
    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 2020-01-05
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多