如何在刀片视图中访问嵌套的子数据

如何在刀片视图中访问嵌套的子数据

本文介绍了Laravel-如何在刀片视图中访问嵌套的子数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在刀片视图中访问模型的嵌套子数据.我的理解是,我需要渴望加载-但不能赢得...

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

学生模型:

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

学生助学金模型:

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

学生助学金注册模型:

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'));

查看:

<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 $学生的控制器产量:

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

具有:

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

我看到学生模型中的

推荐答案

,我将关系助学金,入学率"定义为hasMany关系,因此它将返回实例的集合.但是在您看来,我看到您尝试将其作为单个集合进行访问,因此您在模型上定义的关系方面的问题.关系变为:学生模型:

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');
}

学生助学金模型:

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

在其他情况下,如果您想定义为hasMany关系,则应循环执行助学金"和入学"以访问课程关系

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

这篇关于Laravel-如何在刀片视图中访问嵌套的子数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:02