Laravel 5.3获取关系数据库表的数据


Laravel 5.3 get Data of relational database table

我得到了2表在我的数据库第一个表:"appointments",列Id &用户标识第二张表:users,列id &名称

with blade我可以很容易地显示所有的约会,但我想显示用户名&不仅仅是id。所以当我用blade循环时,我可以很容易地显示{{ $appointment->userid }}的数据但是我现在如何获得这个预约,用户名呢?

appointment类中定义一个关系:

public function user()
{
   return $this->belongsTo('App'User', 'userid');
}

在你的刀片中,你可以这样做:

{{ $appointment->user->name }}

文档