Ali97
Ali97

Reputation: 91

Many to many eloquent query Laravel

I have a students table, a courses table and a student_courses pivot table.

My models are as following:

{
    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);
    }
}

My tables look like this:

        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');

I've successfully added students and courses, and attached students to courses, so I know my models are working.

I am wanting to create a query function in my controller that I can access in a view. I'm wanting a query that gives me a list of each student and what course they're in.

For example

Student1 Course1

Student1 Course2

Student2 Course 1

How can i make a query to use in my view, using a foreach to enter the data into a table? I've successfully outputted all students names and course names, but I don't understand how to query the pivot table

Upvotes: 2

Views: 65

Answers (3)

Lucien Dubois
Lucien Dubois

Reputation: 1704

As you defined the relationship you don't have to manually query the pivot table.

I would do something like that in the controller:

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

As I understand you want to put the data in a table, here is a two columns example:

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

You will get this this of table:

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

Upvotes: 1

Yasin Patel
Yasin Patel

Reputation: 5731

Get data of students with courses

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

in view

@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

Upvotes: 1

Alberto
Alberto

Reputation: 12949

on you controller:

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

and inside the view:

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

Upvotes: 1

Related Questions