madmatuk
madmatuk

Reputation: 127

Laravel 5.0: Querying and displaying child objects in view from nested relationship

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.

Model OptionGroup

class OptionGroup extends Model {

protected $table = 'option_groups';

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function option_choices()
{
    return $this->hasMany('App\OptionChoice');
}

}

Model OptionChoice

class OptionChoice extends Model {

protected $table = 'option_choices';

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function option_group()
{
    return $this->belongsTo('App\OptionGroup');
}

}

Model ModuleQuestion

class ModuleFftQuestion extends Model {

protected $table = 'module_questions';

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function option_group()
{
    return $this->belongsTo('App\OptionGroup');
}
}

Module Controller

....      
$questions = ModuleQuestion::with('option_group.option_choices')
        ->where('enabled', '=', 1)
        ->get()
        ->sortBy('id', true);

return view('modules.index', compact('questions'));

VIEW modules/index.blade.php

....                    
@foreach($questions as $question)
  <section>
    <ul>
       @foreach($question->option_group->option_choices as $choice)
          <li>
             echo something here maybe  {!! $choice->name !!}
          </li>
       @endforeach
     /ul>
   </section>
@endforeach

I would of thought i could do this in the above view but this throws an error:

ErrorException in 35dc55260ec747349284c8d119dae7bf line 17:
Trying to get property of non-object (View:     /www/resources/views/modules/index.blade.php)

If i comment out the nested foreach i can see the queries in laravel debugbar as below:

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')

I would say that my relations are ok as below:

- An option_group has many option_choices
- An option_choice belongs to one option_group
- A module_question belongs to one option_group

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.

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.

Any help will be greatly received.

Regards M

Upvotes: 2

Views: 3009

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

Probably for one of your questions there is no items in option_group so you cannot access option_choices. You should add one extra check this way:

@foreach($questions as $question)
  <section>
    <ul>
       @if ($question->option_group)
           @foreach($question->option_group->option_choices as $choice)
              <li>
                 echo something here maybe  {!! $choice->name !!}
             </li>
           @endforeach
      @endif
     </ul>
   </section>
@endforeach

Upvotes: 3

Related Questions