title: (Laravel) Get data from a table which an ID correspond with another table that link with that table
body: <p>I was playing around with my first Laravel project and my question above might be a little bit confusing but let me explain:-  </p>
  
  <p>I have 3 tables (actually I have more, but lets ignore it), I have <code>standards</code>, <code>stddetails</code> &amp; <code>sections</code> as shown:-  </p>
  
  <p><a href="https://i.stack.imgur.com/Qv7ge.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qv7ge.jpg" alt="Databases"></a></p>
  
  <p>So the foreign key corresponds are below:-</p>
  
  <ul>
  <li><p>column <code>stdPK</code> from table <code>standards</code> = column <code>stdFK</code> from table
  <code>stddetails</code></p></li>
  <li><p>column <code>ssctionPK</code> from table <code>sections</code> = column <code>ssctionFK</code> from<br>
  table <code>stddetails</code></p></li>
  </ul>
  
  <p>And the scenario is this:-</p>
  
  <p>Lets say I <strong>have an <code>$id</code> to be match on <code>stdPK</code> from table <code>standards</code></strong>. <strong>With that <code>$id</code></strong>, I am required to <strong>get all data from table <code>sections</code></strong>. The problem is, I can't find a right query for that as both <code>standards</code> and <code>sections</code> tables only linked with <code>stddetails</code> table.</p>
  
  <p>Currently my query in my <code>web.php</code> is this:-</p>
  
  <pre><code>Route::get('getstddtl/{id?}', function ($id) {
  
       $stdsec = Section::
              leftJoin('stddetails', 'ssctionFK', '=', 'ssctionPK')
              -&gt;join('standards', function($join) use ($id){
                  $join-&gt;on('stdPK', '=', 'stdFK')
                          -&gt;where('stdFK', $id);
              });
  
      return view('standarddtl', [
          'stdsec' =&gt; $stdsec
  
      ]);
  });
  </code></pre>
  
  <p>I thought it should be easy, boy... I was wrong... I hoped someone can help me with this because my brain have very limited thinking capacity.</p>
  
  <p><strong>UPDATE 1:-</strong></p>
  
  <p>I have set the eloquent relationship in each model and uses Laravel's Eloquent method in retrieving the data:-</p>
  
  <pre><code>$stdsec = Stddetail::with('section')-&gt;find($id);
  </code></pre>
  
  <p>All data is retrieved from both <code>stddetails</code> &amp; <code>sections</code> tables, the problem now is difficulty on displaying data from column <code>ssctionName</code> in <code>sections</code> table in a display page as it return an error.</p>
  
  <p>The related code on the display page is below:-</p>
  
  <pre><code>@foreach ($stdsec as $task)
         {{strtoupper($task-&gt;ssctionName)}}
  @endforeach
  </code></pre>
  
  <p>The error shown:-</p>
  
  <p><a href="https://i.stack.imgur.com/6lf5o.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6lf5o.jpg" alt="Error on display foreach"></a></p>
  
  <p>I think the eloquent method is good, now the display part is giving me trouble. Any ideas on how to solve this?</p>
  
  <p><strong>UPDATE 2:-</strong></p>
  
  <p>Here's the models for each table:-</p>
  
  <p>Table <code>standards</code> as <strong>Standard</strong> model:-</p>
  
  <pre><code>namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Standard extends Model
  {
  
      public $primaryKey = 'stdPK';
  
      public function stddetail()
      {
          return $this-&gt;hasMany(Stddetail::class, 'stdFK', 'stdPK');
      }
  }
  </code></pre>
  
  <p>Table <code>stddetails</code> as <strong>Stddetail</strong> model:-</p>
  
  <pre><code>namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Stddetail extends Model
  {
      public $table = "stddetails";
      public $primaryKey = 'sdtlPK';
      protected $fillable = ['stdFK', 'sdtlPgNo', 'sdtlLnNo', 'sdtlText', 'sdtlShrtnote', 'sdtlSchm', 'svrFK', 'ssctionFK', 'sdtlRefLbl'];
  
      public function standard()
      {   
          return $this-&gt;belongsTo(Standard::class, 'stdFK');
      }
  
      public function section()
      {   
          return $this-&gt;belongsTo(Section::class, 'ssctionFK');
      }
  
  }
  </code></pre>
  
  <p>Table <code>sections</code> as <strong>Section</strong> model:-</p>
  
  <pre><code>namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Section extends Model
  {
      public $primaryKey = 'ssctionPK';
      public function stddetails()
      {
          return $this-&gt;hasMany(Stddetail::class, 'ssctionFK', 'ssctionPK');
      }
  }
  </code></pre>