雄辩-在共享的一对多关系中访问父模型的属性


eloquent - accessing the property of parent model in shared one to many relationships

我有以下雄辩的模型:

[用户]

public function studentProfile()
{
        return $this->hasOne('App'StudentProfile');
}
public function tutorProfile()
{
        return $this->hasOne('App'TutorProfile');
}

[学生档案]

public function user()
{
        return $this->belongsTo('App'User');
}
public function tutorialLogs()
{
        return $this->hasMany('App'TutorialLog', 'student_profile_id');
}

〔教程简介〕

public function user()
 {
        return $this->belongsTo('App'User');
 }
 public function tutorialLogs()
 {
        return $this->hasMany('App'TutorialLog', 'tutor_profile_id');
 }

[教程日志]

public function tutorProfile()
{
        return $this->belongsTo('App'TutorProfile');
}
public function studentProfile()
{
        return $this->belongsTo('App'StudentProfile');
}

我的数据库结构也是这样的:

[用户]

id
fname

[学生档案]

id
notes
user_id

[tutor_profiles]

id
speciality
user_id

[tutorial_logs]

id
notes
student_profile_id
tutor_profile_id

我正在尝试基于tutor_profile_id获取导师的教程日志。我已经做到了,但当我要得到导师或学生的名字时,我不能。

这是我的代码:

$tutor = User::whereId(Auth::user()->id)->first();
// get the tutorial logs
$tutorial_logs = $tutor->tutorProfile->tutorialLogs()->orderBy('date')->get();

我不知道如何访问视图中的学生fname或导师fname。

有什么帮助吗?感谢

您可以访问的学生fname

{{ $tutor->student_profile->user->fname }}

导师fname您可以访问

{{ $tutor->user->fname }}