简体   繁体   English

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

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

I have the following eloquent models: 我有以下雄辩的模型:

[User] [用户]

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

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

[StudentProfile] [StudentProfile]

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

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

[TutorProfile] [TutorProfile]

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

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

[TutorialLog] [TutorialLog]

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

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

My database structure also is like this: 我的数据库结构也像这样:

[users] [用户]

id
fname

[student_profiles] [student_profiles]

id
notes
user_id

[tutor_profiles] [tutor_profiles]

id
speciality
user_id

[tutorial_logs] [tutorial_logs]

id
notes
student_profile_id
tutor_profile_id

I am trying to get the tutorial logs for a tutor based on the tutor_profile_id. 我正在尝试基于tutor_profile_id获取一名教师的教程日志。 I have done that, however when I am going to get the tutor name or student name I can't. 我已经做到了,但是当我要获得教师姓名或学生姓名时,我做不到。

Here is my code: 这是我的代码:

$tutor = User::whereId(Auth::user()->id)->first();

// get the tutorial logs
$tutorial_logs = $tutor->tutorProfile->tutorialLogs()->orderBy('date')->get();

I don't know how to access the student fname or tutor fname in the view. 我不知道如何在视图中访问学生的fname或导师的fname。

Any help? 有什么帮助吗? thanks 谢谢

The student fname you can access: 您可以访问的学生fname

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

The tutor fname you can access 您可以访问的教师fname

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM