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

时间:2019-09-10 13:20:59

标签: laravel eloquent laravel-blade

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

学生有助学金,可以有入学金,可以有课程

学生模型:

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

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

1 个答案:

答案 0 :(得分:1)

在我的学生模型中,

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

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

学生助学金模型:

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

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