【问题标题】:Triple Pivot Challenge三重轴挑战
【发布时间】:2015-12-21 11:27:00
【问题描述】:

我有以下挑战,尝试在 Laravel 5 中加入 3 个表:

我在 3 个表之间存在多对多关系:学生、教室、学期。我很想拨打term->classroom->students之类的电话。

我看到了 Eloquent-triple-pivot 包,但不幸的是,我无法让它在 Laravel 5 上运行。

拜托,我会很感激绑定这个并拨打电话的解决方案,无论是雄辩的还是其他的。

谢谢。

编辑

我在 3 个表之间有 多对多关系;教室、学生、学期。

我有 3 个数据透视表:classic_term、classic_student、student_term。

还有3个对应的模型:ClassroomTerm、ClassroomStudent、StudentTerm。

我的教室模型:

 public function students()
{
    return $this->belongsToMany('App\Student')->withTimestamps();
}
public function terms()
{
    return $this->belongsToMany('App\Term')->withTimestamps();
}

我的术语模型:

public function classrooms()
{
    return $this->belongsToMany('App\Classroom')->withTimestamps();
}
public function students()
{
    return $this->belongsToMany('App\Student')->withTimestamps();
}

我的学生模型:

public function terms()
{
    return $this->belongsToMany('App\Term')->withTimestamps();
}
public function classrooms()
{
    return $this->belongsToMany('App\Classroom')->withTimestamps();
}

我在 StudentsController 中所做的是

$term = Term::findOrFail($id);
foreach($term->classrooms as $classroom) {
$studentids = ClassroomStudent::where('classroom_id', '=', $classroom->id)->get();
}
//to get the real student collection
foreach($studentids->student_id as $studentid){
$students = Student::findOrFail($studentid)
}
return view('students.list')->with(['students' => $students]);

在我的 students.list 视图中,我只想检索学生:

@foreach($students as $student)
First Name: {{!!$student->first_name!!}}
@endforeach

错误在这一行:

foreach($studentids->student_id as $studentid){
$students = Student::findOrFail($studentid)
}

Undefined property: Illuminate\Database\Eloquent\Collection::$student_id'

然而 dd($studentids) 返回一个数组,其中一个属性是 student_id。

代码繁琐,无法正常工作。 感谢您的帮助

目标: 检索学期中课堂上的学生。教室、学生和学期之间具有多对多的关系。

【问题讨论】:

  • 你遇到什么样的错误?你的代码是什么..?因为你不能简单地做term->classroom->students你首先做term->classroom通过foreach循环它然后做classroom->students。多对多返回一个数组
  • @Joost 请检查我的 cmets。感谢您的进一步澄清。
  • 如果您向我们展示您的表格字段,我们可以为您提供更好的帮助

标签: php laravel laravel-5


【解决方案1】:

我认为你可能设置的关系不好。试着好好想一想,例如,我认为可能是学生belongsTo 一个教室。

课堂模式

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


public function terms()
{
    return $this->belongsTo('App\Term')->withTimestamps();
}

术语模型:

public function classrooms()
{
     return $this->hasMany('App\Classroom')->withTimestamps();
}

学生模型:

 public function classrooms()  
 {
     return $this->belongsTo('App\Classroom')->withTimestamps();
 }

【讨论】:

    【解决方案2】:

    多对多关系返回一个数组,当您定义 sudentids 时它仍然会返回一个数组 ->get() 也只返回一个数组 ->first() 不会返回一个数组,但是您不会得到所有学生。 变量不会自动转入数组,从而使您的 foreach 仅返回最后一个值。 因此,要正确返回学生的值,您很可能会得到这样的结果。

    控制器:

    // Or even bind the term to the url
    $term = Term::findOrFail($id);
    return view('students.list',compact("term"));
    

    查看:

    <!-- You could even remove the classroom name/list around it if you purely want to display all students under the term -->
    <li>
        @foreach($term->classrooms as $classroom) {
    
            <ul>
                Classroom: {{ $classroom->name }}
                <li>
                    @foreach($classroom->students as $student)
                        <ul>
                            First name: {{ $student->first_name }}
                        </ul>
                    @endforeach
                </li>
            </ul>
    
        @endforeach
    </li>
    

    另一种返回一个学期下所有学生的简单方法是在查询中使用 ->whereHas() 这样做就足够了

    控制器:

    $students = Student::whereHas("classrooms",function($q){
            $q->whereHas("terms",function($q){
                $q->where("id",$termId);
            });
        })->get();
    return view('students.lists',compact("students"));
    

    查看:

    <li>
        @foreach($students as $student)
            <ul>
                First name: {{ $student->first_name }}
            </ul>
        @endforeach
    </li>
    

    如果您想在一个学期内返回特定的教室,甚至返回两者下的用户,您应该为每个控制器创建 3 个控制器(如果您不想将学生分开显示,则只创建 2 个)并根据条款嵌套您的教室,以便您只需输入/terms/1/classrooms/2/ 即可获得正确的教室,或输入/terms/1/classrooms 即可获得一个学期内的所有教室。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      相关资源
      最近更新 更多