我有一个"艺术家"表和一个"表演"表。性能表具有作为时间戳的性能时间字段。
当返回我的艺术家数据时,我还做了一个关系来获得表演:
$artists = Artist::with('performances', 'performances.stage')->get();
然而,我现在想做的是向性能关系添加另一点信息,该信息将使用性能时间字段显示到性能的时间。
我在其他地方使用不同格式的字段数据,所以不想使用赋值函数。
谢谢!
您仍然可以使用赋值函数。假设您的Performance
模型有一个time
字段。
public function getModifiedTimeAttribute() {
return $this->time . ' and something else!';
}
您可以将其作为$performance->modified_time
访问。默认情况下,这个虚构的字段不会存在于模型的Laravel数组/JSON输出中,但您可以将其添加到模型中,使其显示在那里:
protected $appends = ['modified_time'];
您可以在Performance
模型中使用动态属性,如下所示:
class Performance extends Model
{
[...]
public function getTimeToNextAttribute()
{
//calculate Time to next Performance and return it
}
}
然后,您可以通过$performance->time_to_next访问此属性。