简体   繁体   中英

How to display array into blade [Laravel 5]

In my modal I have finction getMyGroups like this:

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

This will be returnet to the Controller:

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

So in blade I need to get:

This key $group -> name from first function in h1 tag and values into p tag, like this:

<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>
...

Array from $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 [▶]
    }
  ]
]

Blade has a @foreach() which should get you started:

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

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

You will need to adjust the markup depending on your array structure. Possible using a nested @foreach to display the paragraphs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM