title: Laravel 5.0: Querying and displaying child objects in view from nested relationship
body: <p>i am trying to access nested data in my blade view, yet everything i try seems to cause one error or another. I am guessing it has to do with the nested related data being a collection, yet i thought if i looped through this i could access what i needed. Code as below.</p>
  
  <p><strong>Model OptionGroup</strong></p>
  
  <pre><code>class OptionGroup extends Model {
  
  protected $table = 'option_groups';
  
  /**
   * @return \Illuminate\Database\Eloquent\Relations\HasMany
   */
  public function option_choices()
  {
      return $this-&gt;hasMany('App\OptionChoice');
  }
  
  }
  </code></pre>
  
  <p><strong>Model OptionChoice</strong></p>
  
  <pre><code>class OptionChoice extends Model {
  
  protected $table = 'option_choices';
  
  /**
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function option_group()
  {
      return $this-&gt;belongsTo('App\OptionGroup');
  }
  
  }
  </code></pre>
  
  <p><strong>Model ModuleQuestion</strong></p>
  
  <pre><code>class ModuleFftQuestion extends Model {
  
  protected $table = 'module_questions';
  
  /**
   * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
   */
  public function option_group()
  {
      return $this-&gt;belongsTo('App\OptionGroup');
  }
  }
  </code></pre>
  
  <p><strong>Module Controller</strong></p>
  
  <pre><code>....      
  $questions = ModuleQuestion::with('option_group.option_choices')
          -&gt;where('enabled', '=', 1)
          -&gt;get()
          -&gt;sortBy('id', true);
  
  return view('modules.index', compact('questions'));
  </code></pre>
  
  <p><strong>VIEW modules/index.blade.php</strong></p>
  
  <pre><code>....                    
  @foreach($questions as $question)
    &lt;section&gt;
      &lt;ul&gt;
         @foreach($question-&gt;option_group-&gt;option_choices as $choice)
            &lt;li&gt;
               echo something here maybe  {!! $choice-&gt;name !!}
            &lt;/li&gt;
         @endforeach
       /ul&gt;
     &lt;/section&gt;
  @endforeach
  </code></pre>
  
  <p>I would of thought i could do this in the above view but this throws an error:</p>
  
  <pre><code>ErrorException in 35dc55260ec747349284c8d119dae7bf line 17:
  Trying to get property of non-object (View:     /www/resources/views/modules/index.blade.php)
  </code></pre>
  
  <p>If i comment out the nested foreach i can see the queries in laravel debugbar as below:</p>
  
  <pre><code>select * from `module_questions` where `enabled` = '1'
  select * from `option_groups` where `option_groups`.`id` in ('2', '0', '3', '4', '5', '1')
  select * from `option_choices` where `option_choices`.`option_group_id` in ('1', '2', '3', '4', '5')
  </code></pre>
  
  <p>I would say that my relations are ok as below:</p>
  
  <pre><code>- An option_group has many option_choices
  - An option_choice belongs to one option_group
  - A module_question belongs to one option_group
  </code></pre>
  
  <p>I am obviously missing something, something hasn't quite clicked for me yet, I am new to Laravel and learning all the time, but this has me stumped.</p>
  
  <p>Is it just how i am trying to access the option_choice data in the view, or is there a better way to do the query with eloquent that will make it easier to access the option_choice data in the view.</p>
  
  <p>Any help will be greatly received.</p>
  
  <p>Regards
  M</p>