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

Laravel 5.3 get Data of relational database table

我的数据库中有 2 个 Table 首先 Table: "appointments" 包含列 Id 和 userid 第二个 Table:"users" 列 id 和名称

与 blade 我可以轻松显示所有约会,但我想显示用户名,而不仅仅是 ID。所以当我用 blade 循环它时,我可以很容易地用 {{ $appointment->userid }} 显示数据 但是现在我怎样才能得到这个约会的用户名呢?

将您的 appointment class 中的关系定义为:

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

然后在你的blade中你可以这样做:

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

Docs