title: How to access the values of a foreign key of another table using laravel
body: <p>I created a custom pivot model which is SectionSubject that have an id, section_id and subject_id. I want now to access the other values related to the section_id. Here is my Sectionsubject model and section_subject table:</p>
  
  <p>SectionSubject.php</p>
  
  <pre><code>class SectionSubject extends Model
  {
      protected $table = 'section_subject';
  
      public function students()
      {
          return $this-&gt;belongsToMany(Student::class, 'section_subject_student','section_subject_id'
              )-&gt;withTimestamps();
      }
  
      public function assignStudents(Student $student)
      {
          return $this-&gt;students()-&gt;save($student);
      }
  
  }
  </code></pre>
  
  <p>create_section_subject.php</p>
  
  <pre><code>Schema::create('section_subject', function (Blueprint $table) {
    $table-&gt;increments('id');
    $table-&gt;integer('section_id')-&gt;unsigned();
    $table-&gt;integer('subject_id')-&gt;unsigned();
    $table-&gt;string('schedule')-&gt;unique();
    $table-&gt;string('room_no');
  
    $table-&gt;foreign('section_id')
       -&gt;references('id')
       -&gt;on('sections')
       -&gt;onDelete('cascade');
  
    $table-&gt;foreign('subject_id')
       -&gt;references('id')
       -&gt;on('subjects')
       -&gt;onDelete('cascade');
  });
  </code></pre>
  
  <p>Now I want to fetch those values and place it on my ReservationController. I tried to use one to many relationship but it seems not working.</p>
  
  <pre><code>class ReservationController extends Controller
  {
  
      public function index()
      {
          $sectionSubject = SectionSubject::find(1);
  
          //code here
          return view('reservation.form', compact('sectionSubject');
      }
  
  } 
  </code></pre>