【发布时间】:2020-01-12 06:25:53
【问题描述】:
试图在刀片视图中访问模型的嵌套子数据。我的理解是我需要急于加载 - 但不是获胜......
学生有助学金,可以有招生,可以有课程
学生模型:
public function bursaries() {
return $this->hasMany('App\StudentBursary');
}
学生助学金模式:
public function enrolments() {
return $this->hasMany('App\StudentBursaryEnrolment');
}
学生助学金招生模式:
public function courses() {
return $this->hasMany('App\StudentBursaryEnrolmentCourse');
}
控制器(具体功能内容...):
$students = $bursary_administrator->students()->where([['status','=',1]])->with('bursaries.enrolments.courses')->get();
return view('baadmin.reports.active_students', compact('report_title','bursary_administrator','students'));
查看:
<table class="table">
<thead>
<tr>
<th>Student Name</th>
<th>Student Middle Names</th>
<th>Student Surname</th>
<th>Bursary Provider Reference</th>
<th>Passport Number</th>
<th>Passport Expiration</th>
</tr>
</thead>
<tbody>
@if ($students)
@foreach ($students as $student)
<tr>
<td>{{$student->student_name}}</td>
<td>{{$student->student_middle_names}}</td>
<td>{{$student->student_surname}}</td>
<td>{{$student->bursary_provider_reference}}</td>
<td>{{$student->passport_number}}</td>
<td>{{$student->passport_expiration}}</td>
</tr>
<tr>
<td colspan="6">
<table class="table">
<tbody>
@if ($student->bursaries->enrolments->courses)
@foreach ($student->bursaries->enrolments->courses as $course)
<td>{{$course->course}}</td>
<td>{{$course->qualification}}</td>
<td>{{$course->commencement_date}}</td>
<td>{{$course->completion_date}}</td>
@endforeach
@endif
</tbody>
</table>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
与:
$students = $bursary_administrator->students()->where([['status','=',1]])->with('bursaries.enrolments.courses')->get();
dd $students 在控制器产量:
Collection {#1568 ▼
#items: array:170 [▼
0 => Student {#1396 ▼
#fillable: array:20 [ …20]
#statuses: array:6 [ …6]
#connection: "mysql"
#table: "students"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:21 [ …21]
#original: array:25 [ …25]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [ …2]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [ …1]
}
与:
$students = $bursary_administrator->students()->where([['status','=',1]])->with('bursaries','bursaries.enrolments','bursaries.enrolments.courses')->get();
dd 产量:
Collection {#1568 ▼
#items: array:170 [▼
0 => Student {#1396 ▼
#fillable: array:20 [ …20]
#statuses: array:6 [ …6]
#connection: "mysql"
#table: "students"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:21 [ …21]
#original: array:25 [ …25]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:2 [ …2]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [ …1]
}
【问题讨论】:
-
目前的结果是什么?错误还是空白?
-
你能在你的控制器中
dd($students);并提供你在关系中得到的东西吗? -
@SeverinDK - 错误:此集合实例上不存在属性 [enrolments]。 (观点:...
-
@mare96 - 检查 OP 末尾以获取所需的其他信息
标签: laravel eloquent laravel-blade