需要将计算属性附加到模型中:
class Field extends Model {
//...
protected $appends = ['typename'];
//...
public function getTypenameAttribute(): string {
return $this->type->name;
}
}
模型转储向我显示以下内容:
{
"id": "9bc5b05b-46cf-470d-a848-cdb60aec2213",
"name": "Показание термометра",
"default": true,
"twin_id": "9bc5b05b-3df3-4efd-8ce0-7a30be87af72",
"type_id": "9bc3cea3-47e6-4ef2-a78f-347d51abbe3d",
"created_at": "2024-04-10T07:21:36.000000Z",
"updated_at": "2024-04-10T07:21:36.000000Z",
"deleted_at": null,
"origin": "thermo",
"typename": "Целочисленное без знака",
"type": {
"id": "9bc3cea3-47e6-4ef2-a78f-347d51abbe3d",
"name": "Целочисленное без знака",
"influx_type": "uint",
"created_at": "2024-04-09T08:54:37.000000Z",
"updated_at": "2024-04-09T08:54:37.000000Z",
"deleted_at": null
}
}
typename
计算好了。但我不想包含嵌套type
对象。我的模型类不包含任何$with
,所以这个对象完全出乎意料。
如何从转储中排除这个嵌套对象?
请确保您的关系模型看起来像这样
}
用法