我有这4张表:
events (id, name, date)
drivers (id, first_name, last_name)
participants (id, number, event_id, driver_id, codriver_id)
penalties (id, event_id, participant_id)
如何在显示惩罚表时获取驱动程序/辅助驱动程序first_name和last_name?
我尝试过这样做,但不起作用:
$grid->participant()->driver()->full_name();
$grid->participant()->codriver()->full_name();
惩罚模型:
public function participant()
{
return $this->belongsTo(Participant::class);
}
参与者模型:
public function driver()
{
return $this->belongsTo(Driver::class);
}
public function codriver()
{
return $this->belongsTo(Driver::class);
}
驱动程序模型:
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
我该怎么做呢?
谢谢
发布于 2019-06-02 06:19:41
->relation_name()
返回关系查询构建器:
要获取关系对象,应将关系作为属性调用,而不是作为函数调用(与full_name属性相同):
$grid->participant->driver->full_name;
它相当于:
$grid->participant()->first()->driver()->first()->full_name;
https://stackoverflow.com/questions/56410685
复制相似问题