【问题标题】:accessing collections within collections laravel 5访问集合 laravel 5 中的集合
【发布时间】:2018-03-23 12:59:24
【问题描述】:

我有一个“名册”表,它几乎完全是外键。它访问“学校”表、“课程”表和“学生”表。因此,从本质上讲,“学生”在“学校”上“课程”。我的 RostersController 有这个

public function show($id)
{
   $roster = Roster::where('course_id', $id);
   return view('roster')->with('roster', $roster);
}

我的花名册模型是:

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

我的学生模型是:

public function roster()
{
  return $this->belongsTo('App\Roster');
}

我的看法是这样的:

@foreach ($roster as $child)
  <p>{{$child->id}}</p>
  <p>{{$child->students->first_name}}</p>
@endforeach

rosters 表只保存 student_id,而不是保存在“students”表中的所有学生数据。所以我试图从这个关系中访问学生表,但是当我运行它时,它告诉我任何与学生表相关的东西都“不在这个集合中”。我知道如果我使用 hasOne 关系,我可以这样做,但是我如何使用 hasMany 来完成此操作以在每一行中输出学生表值?

【问题讨论】:

    标签: laravel eloquent blade


    【解决方案1】:

    你应该试试这个

    public function show($id)
    {
       $roster = Roster::with('students')->where('course_id', $id);
       return view('roster')->with('roster', $roster);
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      名册.php

      public function show($id)
      {
         $roster = Roster::with('students')->where('course_id', $id)->get(); // load the students
         return view('roster')->with('roster', $roster);
      }
      

      在视图中

      @foreach ($roster as $child)
        <p>{{$child->id}}</p>
        <p> 
         <!-- Loop through the stundents since this returns an collection of students and not just one -->
         @foreach($child->students as $student)
          {{$student->first_name}} <br>
         @endforeach
         </p>
      @endforeach
      

      查看this 了解有关预加载的更多信息

      【讨论】:

      • 我很抱歉,我用最少的代码来得到我需要的答案,但我需要提供更多细节。我实际上将其输出到表格行。所以 child->id 和 $student->first_name 需要在同一行输出上。如果我按照我最初发布的方式完全遵循您的代码,它不会输出一个孩子-> id,然后是每个学生吗?甚至与那个孩子-> id 无关的那些?我问是因为我需要对其进行更改以适应我的餐桌需求。我怎样才能从学生表中只获取属于该孩子的学生->id?
      • 当您执行Roster::with('students')... 时,您是在说您想获取您的Roster,它会带来一个名为students 的属性,该属性将是属于的所有学生的数组Roster 模型。所以在每个 $roster 中,它都有一个学生数组
      • 正如我最后所说,检查这个文档,因为他们解释得比我能做到的更好laravel.com/docs/5.6/eloquent-relationships#eager-loading
      【解决方案3】:

      $child-&gt;students 是一个集合。因此,您需要在第一个循环中使用另一个 foreach 循环。

      您的代码应如下所示:

      @foreach ($roster as $child)
          <p>{{$child->id}}</p>
              @foreach($child->students as $student)
                  <p>{{$student->first_name}}</p>
                  <p>{{$student->age}}</p>
                  <p>{{$sutdent->another_column_in_students_table}}</p>
              @endforeach
           <p>{{$child->any_other_column_in_roster_table_if_needed}}</p>
      @endforeach
      

      【讨论】:

        【解决方案4】:

        所以,你的第一个问题是,那个

        $roster = Roster::where('course_id', $id);
        

        只会返回一个 QueryBuilder 实例,你必须使用

        $roster = Roster::where('course_id', $id)->get();
        

        那么可以肯定,students 是一个集合,你必须像这样迭代它:

        @foreach ($child->students as $student)
            {{ $student->yourProperty}} <br>
        @endforeach
        

        更新: 我看到你已经知道了when to use get() in query laravel 5

        【讨论】:

        • 抱歉不彻底,我实际上在控制器的代码中有 get(),我只是忘记在问题中输入它。我没有像你在那儿那样尝试将 'students' 链接到 '$child' 的入口变量。我会试一试。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        • 2019-12-01
        • 2015-06-06
        • 2020-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多