0

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

2 Answers 2

1

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

Also, add this to relation:

->withPivot('section_code')

More on getting data from pivot tables here.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. But Im having this error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'section_subject.section_code' in 'field list' (SQL: select sections.*, section_subject.subject_id as pivot_subject_id, section_subject.section_id as pivot_section_id, section_subject.section_code as pivot_section_code from sections inner join section_subject on sections.id = section_subject.section_id where section_subject.subject_id in (1, 2, 3))
Show relationships please.
You didn't add ->withPivot('section_code') to relation, please try to do that.
No in my project code I already did that but im having the error as mention in my above comment.
I just remove it from the question above. But I already added it.
0

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.

1 Comment

Thanks but I think you dont need to create a SectionSubject model. You just need to create a pivot table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.