在我看来,我需要通过 for each 循环获取用户拥有的所有课程。我试图以这种方式建立关系:User.php
public function user_courses() {
return $this->hasMany(User_Course::class)->orderBy('created_at');
}
课程.php
public function user_course() {
return $this->belongsTo(User_Course::class);
}
用户课程.php
class 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个
蛊毒传说