laravel - 模型隐藏修改器和嵌套字段
问题描述
这是我的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
是我创建的一个增变器,用于将其作为属性附加。
解决方案
推荐阅读
- wordpress - IIS 上的 WordPress 需要通配符 301 重定向
- mysql - 如何将从 Homebrew 安装的 mysql-client 与diesel-cli 链接?
- android - DeviceOrientation API 在 Cordova/Android 上不起作用
- c# - Oracle 通知小巧玲珑
- terraform - terraform 在多个 aws 实例上设置主机名
- string - 如何在 Agda 中加入字符串
- python - 在子流程之间传递用户定义的对象
- javascript - JavaScript 或 PHP:列出 HTML5 文件夹中的所有 mp3 文件
- javascript - 即使某些未选中,如何将单选按钮类型测验的答案保存到 Firebase 数据库
- clockify - 获取已保存报告(按分组)的时间条目的最佳时钟 API 端点是什么?