在我看来,我需要通过 for each 循环获取用户拥有的所有课程。我试图以这种方式建立关系:User.phppublic function user_courses() { return $this->hasMany(User_Course::class)->orderBy('created_at');}课程.phppublic function user_course() { return $this->belongsTo(User_Course::class);}用户课程.phpclass User_Course extends Model{ protected $guarded = []; public function user() { return $this->belongsTo(User::class); } public function course(){ return $this->hasOne(Course::class,'id'); }}仪表板.blade.php@foreach($user->user_courses as $course) {{ dd($course) }} <div class="course-flex-item"> <div class="course-header"> <strong>{{ $course['title'] }}</strong> </div> <div class="course-description"> </div> <div class="continue-button"> <button>Continue</button> </div> <div class="image-container"> <img class="course-image" src="https://www.w3.org/html/logo/downloads/HTML5_Badge_256.png" alt="fasdf"> <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width: %;background-color: #f64c71;font-family:'Montserrat';font-weight:bold"> <span class="">%</span> </div> </div> </div> </div> @endforeach我试图通过 course_id 获取每门课程的详细信息。例如,如果 id 为 3 的用户拥有 course_id 为 2 的课程,则数据库行将如下所示:id|user_id|course_id 1 | 3 | 2个
1 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
您可以执行以下操作:在用户模型中,您需要创建此功能
public function courses()
{
return $this->belongsToMany(Course::class, 'user_courses','user_id','course_id');
}
- 1 回答
- 0 关注
- 91 浏览
添加回答
举报
0/150
提交
取消