title: stacking relationships to get count() laravel 5
body: <p>I have 4 tables: schools, courses, rosters, students. The "schools" hasMany courses and hasMany "students". The rosters hasMany "students". The courses hasOne "rosters". Right? I'm confused too. So in my controller, I queried the schools table, and it gives me access to "one level" of relationships. Meaning I can do $schools->courses->date or $schools->students->first_name etc... in my @foreach loops. Here's where i'm getting lost. The rosters table is basically an entire table of foreign key relations. It has no unique data on its own other than the timestamps and primary ID. I have a table where i'm trying to display the all the courses for a school, along with the total value of students enrolled in that particular course. So i'm foreach-ing every course for the school, easy enough. But then i'm trying to pull the count() from the rosters table for every student in that course and reflect it on that table row. </p>
  
  <p>So to recap, one table row per course. Trying to get the students enrolled in each respective course. My controller query is basically just </p>
  
  <pre><code>$schools::School::findOrFail($id);
  </code></pre>
  
  <p>From that, i can get all the course and student info, but i can't figure out how to basically stack the relationship to equal something like this:</p>
  
  <p>school->course->roster->total number of students.</p>
  
  <p>I'm not even trying to keep this all in the same query. I can't even figure out how to do a query in the controller and just pass a final value in a variable along to the view. I'd be totally ok with doing a second query in the controller to get the value of the rosters but i can't wrap my head around multiple courses per school in one query with one roster per course. I'm confusing myself typing this question lol. Can anyone offer advice?</p>
  
  <p><a href="https://i.stack.imgur.com/dAKnd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dAKnd.png" alt="enter image description here"></a></p>
  
  <pre><code>class Roster extends Model
  {
      public function courses()
      {
          return $this-&gt;belongsTo('App\Course');
      }
  
      public function students(){
          return $this-&gt;hasMany('App\Student', 'student_id', 'id');
      }
  }
  
  class Course extends Model
  {
      public function schools()
      {
          return $this-&gt; belongsTo('App\School');
      }
  
      public function roster()
      {
          return $this-&gt;hasOne('App\Roster', 'class_id', 'id');
      }
  
  
  }
  
  class Student extends Model
  {
  
      public function grade(){
  
        return $this-&gt;hasOne('App\Grade');
      }
  
      public function school()
      {
          return $this-&gt;hasOne('App\School');
      }
  
      public function drive()
      {
          return $this-&gt;hasMany('App\Drive');
      }
  
      public function roster()
      {
          return $this-&gt;belongsTo('App\Roster');
      }
  
  }
  
  class School extends Model
  {
      public function courses()
      {
          return $this-&gt;hasMany('App\Course');
      }
  
      public function students()
      {
          return $this-&gt;hasMany('App\Student');
      }
  
      public function employee()
      {
          return $this-&gt;hasMany('App\Employee','school_id', 'id');
      }
  
      public function drive()
      {
          return $this-&gt;hasMany('App\Drive');
      }
      public function car(){
          return $this-&gt;hasMany('App\Car', 'schools_id', 'id');
      }
  }
  </code></pre>