首页 > 解决方案 > 模型隐藏修改器和嵌套字段

问题描述

这是我的JSON返回

{
    "product": {
        "id": 1,
        "name": "Glory Cookware",
        "barcode": "1234567891234",
        "local_code_id": 1,
        "category_id": 1,
        "description": null
    },
    "calculations": {
        "product_quantity_in_warehouses": 1580,
        "avgSellingPriceBeforeTax": 901.7085714285713,
        "avgSellingPriceAfterTax": 1027.9477714285713,
        "creditPriceInWarehousesBeforeTax": 1530828,
        "creditPriceInWarehousesAfterTax": 1745143.92
    }
}

我正在尝试做的事情:

Product.php我试图隐藏的模型中calculations.creditPriceInWarehousesBeforeTax

我试过的:

我尝试在模式下使用toArray()方法。Product.php

public function toArray()
{
    $hidden = ['calculations.creditPriceInWarehousesBeforeTax'];
    // Also tried ['creditPriceInWarehousesBeforeTax']
    $this -> hidden = $hidden;
    return parent::toArray();
}

预期行为 VS 发生了什么

JSON 结果不应该返回,calculations.creditPriceInWarehousesBeforeTax但发生的是它与 JSON 一起返回。

笔记

calculations是我创建的一个增变器,用于将其作为属性附加。

标签: laravel

解决方案


推荐阅读