【发布时间】:2017-08-07 08:16:37
【问题描述】:
欢迎!我制作了用户拥有自己笔记的应用程序(基于外键)。现在在表格中我想显示他的名字但我不能通过它:
试点模型:
class Pilot extends Model
{
protected $table = 'pilots';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'phone', 'email',
];
public function note() {
return $this->hasMany(Note::class);
}
笔记模型:
类注释扩展模型
{
protected $table = 'notes';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'data', 'zadanie', 'uwagi', 'pilot_id',
];
public function pilot() {
return $this->belongsTo(Pilot::class);
}
Notes Controller(我想在索引页面中显示飞行员姓名)
public function index(Request $request)
{
$notes = Note::all();
$pilots = Pilot::find($id);
return view('uwagi.index',['notes'=>$notes,'pilots'=>$pilots]);
}
当我通过它查看时
{{$pilots->name}}
我有一个未定义的 $id 错误。每个便笺基于便笺表中的外键 Pilot_id 属于用户。这个 Pilot_id 键是指表 Pilots -> id,我想在这个 id 的索引页面名称中显示,但我不知道如何传递它。
问候并感谢您的帮助
【问题讨论】:
-
您到底想展示什么?只有一个带有相关注释的飞行员?