【问题标题】:Laravel 5.0: Querying and displaying child objects in view from nested relationshipLaravel 5.0:从嵌套关系中查询和显示视图中的子对象
【发布时间】:2015-03-05 00:58:56
【问题描述】:

我正在尝试访问刀片视图中的嵌套数据,但我尝试的所有操作似乎都会导致一个错误或另一个错误。我猜它与嵌套的相关数据是一个集合有关,但我想如果我循环通过这个我可以访问我需要的东西。代码如下。

模型选项组

class OptionGroup extends Model {

protected $table = 'option_groups';

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

}

模型选项选择

class OptionChoice extends Model {

protected $table = 'option_choices';

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

}

模型模块问题

class ModuleFftQuestion extends Model {

protected $table = 'module_questions';

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

模块控制器

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

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

查看模块/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

我认为我可以在上面的视图中执行此操作,但这会引发错误:

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

如果我注释掉嵌套的 foreach,我可以在 laravel 调试栏中看到如下查询:

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

我会说我的关系还可以,如下所示:

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

我显然错过了一些东西,我还没有完全点击,我是 Laravel 的新手并且一直在学习,但这让我很难过。

这只是我试图访问视图中的 option_choice 数据的方式,还是有更好的方法来使用 eloquent 进行查询,从而更容易访问视图中的 option_choice 数据。

任何帮助都会得到极大的欢迎。

问候 M

【问题讨论】:

    标签: php laravel eloquent foreign-key-relationship laravel-5


    【解决方案1】:

    可能对于您的一个问题,option_group 中没有项目,因此您无法访问 option_choices。您应该以这种方式添加一个额外的检查:

    @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
    

    【讨论】:

    • 嗨,Marcin,谢谢你,确实正是检查或缺乏导致了我的问题,天哪!非常感谢您的提醒! :) M
    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多