Laravel - How to access nested child data in blade view?

我的梦境 提交于 2019-12-11 15:09:27

问题


Trying to access nested child data of a model in a blade view. My understanding is I need to eager load - but not winning...

Student has bursaries, which can have enrolments, which can have courses

Student Model:

public function bursaries() {
    return $this->hasMany('App\StudentBursary');
}

Student Bursary Model:

public function enrolments() {
    return $this->hasMany('App\StudentBursaryEnrolment');
}

Student Bursary Enrolment Model:

public function courses() {
    return $this->hasMany('App\StudentBursaryEnrolmentCourse');
}

Controller (specific function contents...):

    $students = $bursary_administrator->students()->where([['status','=',1]])->with('bursaries.enrolments.courses')->get();

    return view('baadmin.reports.active_students', compact('report_title','bursary_administrator','students'));

View:

<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>

with:

$students = $bursary_administrator->students()->where([['status','=',1]])->with('bursaries.enrolments.courses')->get();

dd $students in controller yields:

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]
}

with:

$students = $bursary_administrator->students()->where([['status','=',1]])->with('bursaries','bursaries.enrolments','bursaries.enrolments.courses')->get();

dd yields:

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]
}

回答1:


in your student model as i see you define relations 'bursaries, enrolments' as hasMany relations so it will return collections of instance . but in your view i see you trying to access to it as single collection ,so the issue on relations you defined on models. the relations become: Student Model:

public function bursaries() {
    return $this->hasOne('App\StudentBursary');
}

Student Bursary Model:

public function enrolments() {
    return $this->hasOne('App\StudentBursaryEnrolment');
}

In other case if you would like to define as hasMany relations you should do loop the 'bursaries' and 'enrolments' to access courses relation



来源:https://stackoverflow.com/questions/57871754/laravel-how-to-access-nested-child-data-in-blade-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!