有一些非常相似的问题,但不是这个问题。让我们从我的模型开始:
Event
,belongsToMany Player
)Event
,belongsToMany Contest
,belongsToMany Event
)Contest
,belongsToMany Players->withPivot('score)
)(实际上称为ContestEvent
,因为Event
是Laravel类)我正在编写一个API来处理数据的其他内容。现在我从给定的比赛中得到这个输出:
{
"id": "1",
"owner_id": "1",
"name": "Some Contest",
"start_date": "2013-08-09",
"end_date": "2013-08-31",
"is_public": "1",
"is_finished": "0",
"players": [
{
"id": "1",
"first_name": "Bob",
"last_name": "Smith",
"initial_name": "Bob S",
"pivot": {
"contest_id": "1",
"user_id": "1"
}
}
],
"events": [
{
"id": "1",
"name": "Stage 1",
"date": "2013-08-01",
"players": [
{
"id": "1",
"first_name": "Bob",
"last_name": "Smith",
"initial_name": "Bob S",
"pivot": {
"contest_event_id": "1",
"user_id": "1",
"score": "5"
}
}
]
}
]
}
pivot
部分是垃圾数据,但score
下的events
部分除外。我可以使用$ hidden隐藏整个pivot
部分,但我希望在score
数组之外的一步中访问pivot
值。
我与$append
和getFooAttribute()
混淆了,但到目前为止没有成功 - 我无法弄清楚如何让Eloquent做我想做的事。有什么建议吗?