【问题标题】:Laravel - How to access nested child data in blade view?Laravel - 如何在刀片视图中访问嵌套的子数据?
【发布时间】: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


【解决方案1】:

在您的学生模型中,我看到您将关系“助学金,注册”定义为 hasMany 关系,因此它将返回实例集合。但在您看来,我看到您试图将其作为单个集合访问,因此您在模型上定义的关系问题。 关系变为: 学生模型:

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

学生助学金模式:

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

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

【讨论】:

  • 谢谢 - hasOne 不是答案,但你答案的最后一句话是。必须单独循环。
猜你喜欢
  • 2023-02-06
  • 2015-06-05
  • 2021-05-11
  • 1970-01-01
  • 2021-01-20
  • 2021-11-06
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多