1

Consider I have the following array (passed to view from my controller):

'first' => [

    '1' => [
        'title' => 'First Div',
        'fields' => [
            'name', 'select'
        ],
    ],

     'divider',

    '2' => [
        'title' => 'Second Div',
        'fields' => [
            'address'
        ],
    ],

Looks like this in the browser when I dump:

array (size=2)
  1 => 
    array (size=2)
      'title' => string 'First Div' (length=9)
      'fields' => 
        array (size=2)
          0 => string 'name' (length=4)
          1 => string 'select' (length=6)
  2 => 
    array (size=2)
      'title' => string 'Second Div' (length=10)
      'fields' => 
        array (size=1)
          0 => string 'address' (length=7)

What I am trying to do is the following. I want to loop through all arrays, then display a div with a title, and within that div, loop through all the fields for each array.

For now I am focussing on just the looping through the array and displaying the right data, so without any divs yet. So something like this:

@foreach($first as $key => $value)

    // Unsure what to do here

@endforeach

I've tried a lot of things (just playing around trying to dump or echo out data), and I cannot even figure out how to display the title, let alone loop through all the fields values.

Either way, my ultimate goal is to create the following, dynamic view, based on what is passed from the controller:

Example

1
  • 3
    Well $value will also be an array, so add another foreach loop to process that Commented Aug 15, 2018 at 9:48

1 Answer 1

4

To echo out a value in blade you can wrap your variable in {{ }}.

You should be able to do something along the lines of:

@foreach($first as $key => $value)

    <div>
        <h2>{{ $value['title'] }}</h2>

        @foreach($value['fields'] as $field)

            {{ $field }} <br>

        @endforeach
    </div>

@endforeach

Please view the documentation for more information

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I know about the brackets, and honest to god I've tried what you described here and I only got errors. But this somehow magically works. Haha thanks a lot, lots to learn for me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.