如何将阵列显示到刀片[Laravel 5]

时间:2015-05-28 14:37:56

标签: php arrays laravel-5

在我的模态中,我有这样的结尾getMyGroups

foreach($groups as $group){
  $questions[] = array($group -> name => Question::where('group_id',$group-> group_id));
 }
  return $questions;

这将是回归控制者:

if(is_numeric($id)){
  return view('project',array('groups' => $mygroups->getMyGroups($id)));  
}else{
  return redirect('home');
     }

所以在刀片中我需要得到:

此键$group -> name来自h1标记中的第一个函数,值变为p标记,如下所示:

<h1> This is first key </h1>

<p>this is 1. value for first key</p>
<p>this is 2. value for first key</p>
<p>this is 3. value for first key</p>
...

<h1> This is second key </h1>

<p>this is 1. value for second key</p>
<p>this is 2. value for second key</p>
<p>this is 3. value for second key</p>
...

来自$ groups的数组:

array:2 [▼
  0 => array:1 [▼
    "key123" => Builder {#213 ▼
      #query: Builder {#218 ▶}
      #model: Question {#217 ▶}
      #eagerLoad: []
      #macros: []
      #onDelete: null
      #passthru: array:12 [▶]
    }
  ]
  1 => array:1 [▼
    "key2" => Builder {#215 ▼
      #query: Builder {#207 ▶}
      #model: Question {#208 ▶}
      #eagerLoad: []
      #macros: []
      #onDelete: null
      #passthru: array:12 [▶]
    }
  ]
]

1 个答案:

答案 0 :(得分:1)

Blade有一个@foreach(),可以帮助你入门:

@foreach ($groups as $group => $name)
    <h1> This is {{ $name }} </h1>

    <p>this is ...</p>
@endforeach

您需要根据阵列结构调整标记。可以使用嵌套的@foreach来显示段落。