skip to Main Content

I want to return my nested array in a foreach loop. In this foreach loop I need to add a foreach, because it is variable how many accordions exist.

But I can’t get it to load the data from the "accordion" array into the frontend.
There is always an error somewhere.

I can’t describe it any better right now.

Save

This is my treatments.php file:

'treatments' => [
        [ /*  */
            'img' => asset('img/logos/Dermatologie_Dr_med_Aresu_Naderi_Nienstedten_Logo.png'),
            'title' => 'Klassische Dermatologie',
            'teaser' => 'Ein wesentlicher Bestandteil im Behandlungskonzept meiner Hautarztpraxis ist die ästhetische Dermatologie und Lasermedizin.',
            'pop-up' => 'derma',
            'accordion' => [
                [
              'title' => 'test',
              'content' => 'test2',
                ],
                [
                    'title' => 'test',
                    'content' => 'test2',
                ],
            ],
        ],
    ]

And this is my Frontend Blade Code:

<div class="row">
        @foreach( $treatments as $key => $value)
            <?php print_r($value) ?>
            <div class="col-md-4 py-5">
                <img class="w-100" src="{!! __($value['img']) !!}" alt="{!! __($value['title'])  !!}">
            </div>
            <div class="col-md-8 py-5">
                <span class="treatmentsHeadline">{!! __($value['title'])  !!}</span>
                <p class="pt-3">{!! __($value['teaser'])  !!}</p>
                <div class="pt-3">
                    <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Mehr erfahren</button>
                    <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Termin vereinbaren</button>
                </div>
            </div>
                @foreach($value as $acc)
                    <p>{!! __($acc['title']) !!}</p>
                @endforeach
        @endforeach
        <p class="footer-vh"></p>

    </div>

And this is the Array printed out:
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I finaly got it!

    @php
        $i = -1;
    @endphp
    <div class="container-fluid px-0">
        <img class="w-100" src="{!! __('treatments.derma') !!}" alt="">
    </div>
    <div class="container my-4">
        <div class="row">
            @foreach( $treatments as $key => $value)
                @php
                    $i++;
                @endphp
    
                <div class="col-md-4 py-5">
                    <img class="w-100" src="{!! __($value['img']) !!}" alt="{!! __($value['title'])  !!}">
                </div>
                <div class="col-md-8 py-5">
                    <span class="treatmentsHeadline">{!! __($value['title'])  !!}</span>
                    <p class="pt-3">{!! __($value['teaser'])  !!}</p>
                    <div class="pt-3">
                        <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Mehr erfahren</button>
                        <button class="button-naderi" href="#{!! __($value['pop-up']) !!}">Termin vereinbaren</button>
                    </div>
                </div>
                @isset($treatments[$i]['accordion'])
    
                    @foreach($treatments[$i]['accordion'] as $key => $acc)
    
                        @php
                            print_r($acc);
                        @endphp
                        <p>{!! __($acc['title']) !!}</p>
                        <p>{!! __($acc['content']) !!}</p>
    
                    @endforeach
                @endisset
            @endforeach
            <p class="footer-vh"></p>
    
        </div>
    </div>
    

  2. @foreach($value as $acc) should be @foreach($value['accordion'] as $acc)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search