【发布时间】:2020-09-23 11:50:05
【问题描述】:
我正在尝试显示所有用户拥有的课程。 课程详细信息显示正常,但是当我尝试使用第二个 foreach 循环获取用户进度时,它为所有课程返回相同的值。 注意:我无法从第一个 foreach 中获取进度,因为进度存储在 user_courses 表中,而其他课程详细信息(例如课程名称和描述)存储在 course 表中。 代码如下:
@foreach($user->courses as $course)
<div class="course-flex-item">
<div class="course-header">
<strong>{{ $course['title'] }}</strong>
</div>
<div class="course-description">
{{ $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">
@foreach($user->user_courses as $user_course)
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="70"
aria-valuemin="0" aria-valuemax="100" style="width:{{ $user_course['progress'] }}%;background-color: #f64c71;font-family:'Montserrat';font-weight:bold">
<span class="">{{ $user_course['progress'] }}%</span>
</div>
</div>
@endforeach
</div>
</div>
@endforeach
例如,如果用户有 2 门课程,其中一门完成率为 12%,另一门完成率为 30%,则这两门课程都显示为 30%。
var_dump($user_courses): 第一道菜:
bject(App\User_Course)#1242 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
12%
object(App\User_Course)#1245 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
第二门课程:
bject(App\User_Course)#1242 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
12%
object(App\User_Course)#1245 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
【问题讨论】:
标签: laravel loops foreach eloquent laravel-blade