【发布时间】:2019-10-23 19:33:36
【问题描述】:
使用 Laravel Nova,我想在 BelongsToMany 子视图中显示一个计算字段。在中间模型中,我设置了一个虚拟属性字段,它使用连接表计算一个值。但是,此字段未显示可能是因为 Nova 检查了 withPivot 字段(无法将其添加到该字段,因为它不是真实字段)。有没有其他方法可以让它工作?
这背后的数据库模型是:
[GameVariation] has BelongsToMany using [GameVariationMatch] with [GameMatch]
[GameMatch] HasOne [Match]
在 GameVariation 资源中,我设置了一个应该显示计算字段的 BelongsToMany 字段:
BelongsToMany::make( 'Game Matches', 'game_matches', GameMatch::class )
->fields( function () {
return [
Text::make( 'Match Data' )->displayUsing(function() {
return $this->match_data;
})
];
} ),
在 GameVariation 模型中,表格与 BelongsToMany 相连:
final public function game_matches(): BelongsToMany {
return $this->belongsToMany(
GameMatch::class
)
->using( GameVariationMatch::class );
}
在数据透视表模型 GameVariationMatch 中,计算字段的设置如下:
final public function getMatchDataAttribute(): string {
return $this
->game_match
->match
->match_data;
}
【问题讨论】:
标签: laravel-nova