【问题标题】:Many to many eloquent query Laravel多对多雄辩查询 Laravel
【发布时间】:2020-03-11 15:06:18
【问题描述】:

我有一个学生表、一个课程表和一个 student_courses 数据透视表。

我的模型如下:

{
    public $timestamps= false;
    public function courses()
    {
        return $this->belongsToMany(Course::class);

    }
}
class Course extends Model
{

    public $timestamps= false;
    public function students()
    {
        return $this->belongsToMany(Student::class);
    }
}

我的表格如下所示:

        Schema::create('courses', function (Blueprint $table) {
            $table->increments('id');
            $table->String('code');
            $table->String('name');
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first');
            $table->string('last');
Schema::create('course_student', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('course_id')->unsigned();
            $table->foreign('course_id')->references('id')->on('courses');
            $table->integer('student_id')->unsigned();
            $table->foreign('student_id')->references('id')->on('students');

我已成功添加学生和课程,并将学生附加到课程,因此我知道我的模型正在运行。

我想在我的控制器中创建一个可以在视图中访问的查询函数。我想要一个查询,它可以为我提供每个学生的列表以及他们所在的课程。

例如

学生1课程1

学生1课程2

学生 2 课程 1

如何使用 foreach 将数据输入到表中来进行查询以在我的视图中使用?我已经成功输出了所有学生姓名和课程名称,但是我不明白如何查询数据透视表

【问题讨论】:

  • 你能出示你的代码吗

标签: php laravel


【解决方案1】:

在你的控制器上:

$students = Student::with('courses')->get();

在视图内部:

@foreach($students as $student)
    @foreach($student->courses as $course )
       {{ $student->first . $student->last }} {{ $course->name }}
    @endforeach
@endforeach

【讨论】:

  • 太完美了,谢谢 一直缺少 ::with 和嵌套的 foreach
【解决方案2】:

获取有课程的学生数据

$students = Student::with('courses')->get();

在视图中

@foreach($students as $student)
    {{ $student->first }} {{ $student->last }} // to display student name

    @foreach($student->courses as $course) // loop of courses of student
      {{ $course->name }} // to display student name
    @endforeach

@endforeach

【讨论】:

    【解决方案3】:

    当您定义关系时,您不必手动查询数据透视表。

    我会在控制器中做类似的事情:

    $students = Student::with('courses')->get();
    

    据我了解,您想将数据放在表格中,这里有一个两列示例:

    <table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Courses</th>
        </tr>
    </thead>
    <tbody>
        @foreach($students as $student)
        <tr>
            <td>{{ $student->first}} {{ $student->last}}</td>
            <td>
                @foreach($student->courses as $course)
                    {{ $course->name }},
                @endforeach
            </td>
        </tr>
        @endforeach
    <tbody>
    </table>
    

    你会得到这张桌子:

    | Student Name | Courses    |
    |:-------------|-----------:|
    | John         | C1, C2, C3 |
    | Marc         | CA, CB, CC |
    

    【讨论】:

    • 看看我的答案...这是多对多,所以一个学生有很多课程
    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 2018-01-26
    • 2021-07-23
    相关资源
    最近更新 更多