Jearson
Jearson

Reputation: 421

How to display values inside pivot table using eloquent in laravel

I have this enrollment project, that a student will only choose the subjects he/she wants to enroll. Those subject will be fetch automatically and be displayed on a table. See this image. After the student will click on the add button of the enrollment form, the subject will be added to the added subjects form. However I am having problem displaying the values of all sections and subjects define on my pivot table which is the section_subject. How can I achieve this? I already define belongsToMany relationships on the Section Model and Subject Model. Here's my code.

Subject.php

class Subject extends Model
{
    public function sections()
    {
        return $this->belongsToMany(Section::class);
    }
}

Section.php

class Section extends Model
{
    public function subjects()
    {
        return $this->belongsToMany(Subject::class);
    }
}

ReservationController class

class ReservationController extends Controller
{
    public function create($id)
    {
        $subjects = Subject::with('sections')->get();

        return view('reservation.form',compact('subjects'));
    }
}

form.blade.php

<tr>
  <th>Section</th>
  <th>Subject Code</th>
  <th>Descriptive Title</th>
  <th>Schedule</th>
  <th>No. of Units</th>
  <th>Room</th>
  <th>Action</th>
  </tr>
  <body>
  @foreach($subjects as $subject)
    <tr>
      <td>{{ $subject->section_code }}</td>
      <td>{{ $subject->subject_code }}</td>
      <td></td>
      <td></td>
      <td>{{ $subject->units }}</td>
      <td>Df</td>
      <td>
         <button class="btn btn-xs btn-primary">Add</button>
         <button class="btn btn-xs btn-info">Edit</button>
      </td>
    </tr>
   @endforeach

Here's the section_subject table

Schema::create('section_subject', function (Blueprint $table) {

  $table->integer('section_id')->unsigned();
  $table->integer('subject_id')->unsigned();

  $table->foreign('section_id')
        ->references('id')
        ->on('sections')
        ->onDelete('cascade');

  $table->foreign('subject_id')
        ->references('id')
        ->on('subjects')
        ->onDelete('cascade');

  $table->primary(['section_id', 'subject_id']);
});

Also, here are the subjects table and section table respectively.

Schema::create('subjects', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('department_id')->unsigned();
  $table->string('subject_code');
  $table->string('subject_description');
  $table->integer('units');
  $table->integer('cost');
  $table->timestamps();
});

Schema::create('sections', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('instructor_id')->unsigned();
  $table->string('section_code');
  $table->string('section_description');
  $table->string('room_no');
  $table->date('schedule');
  $table->date('semester');
  $table->timestamps();
});

Upvotes: 0

Views: 1473

Answers (2)

wupendra
wupendra

Reputation: 86

If I am right you want to get all the subjects and sections that are present in the pivot table. If that is the case then you can make a new model SectionSubject and pull the distinct data from it.

Model SectionSubject:

use Illuminate\Database\Eloquent\Model;

class SectionSubject extends Model {

    public function section()
    {
        return $this->belongsTo(Section::class);
    }
    public function subject()
    {
        return $this->belongsTo(Subject::class);
    }
}

And then in Controller where you need it define this model as:

use App\SectionSubject;

And finally you can query out all the Sections and Subjects as:

$sections = SectionSubject::distinct()->select('section_id')->with('section')->get();
$subjects = SectionSubject::distinct()->select('subject_id')->with('subject')->get();

This gives you all the subjects and sections in the pivot table. I hope it helps you.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

Try {{ $subject->pivot->section_code }}

Also, add this to relation:

->withPivot('section_code')

More on getting data from pivot tables here.

Upvotes: 1

Related Questions