Laravel 嵌套的 foreach 返回重复项
Posted
技术标签:
【中文标题】Laravel 嵌套的 foreach 返回重复项【英文标题】:Laravel nested foreach returns duplicates 【发布时间】: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" >
@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)
【问题讨论】:
【参考方案1】: @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" >
@if(is_array($course)
@foreach($course 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
@endif
</div>
</div>
@endforeach
【讨论】:
Ljaz,这甚至没有打印进度 App\User_Course #1242 ▼ #guarded: [] #connection: "mysql" #table: "user__courses" #primaryKey: "id" #keyType: "int" +incrementing: true #with : [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:6 [▶] #original: array:6 [▶] #changes: [] #casts: [] #classCastCache : [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [ ] #fillable: [] Ljaz 这是它打印的内容 请使用 var dump 或 print_r 它没有显示所有记录 添加了@Naeem Ljaz【参考方案2】:消除第二个 foreach。然后像这样获得进度
$course->user_course->where('user_id', $user->id)->first() != null ? $course->user_course->where('user_id', $user->id)->first()->progress : 0
【讨论】:
以上是关于Laravel 嵌套的 foreach 返回重复项的主要内容,如果未能解决你的问题,请参考以下文章