1

So what I am trying to do is create an FAQ accordion but use a @foreach loop and not copy and paste the entire list. My controller code looks like this:

public function faq(){
    $faqs=array(
       'questions' => ['question1','question2','question3'],
       'answers' => ['answer1','answer2','answer3'],
       'counts' => ['One','Two','Three'] 
   );
  return view('frontend.pages.faq')->with($faqs);
}

This is what I wish to achieve:

@foreach($questions as $question && $answers as $answer && $counts as $count)
    <div id="accordion">
        <div class="card card--faq mb-2">
            <div class="bdv-btn card-header" id="heading{{$count}}">
                <h5 class="mb-0">
                    <a class="btn btn-link w-100" data-toggle="collapse" data-target="#collapse{{$count}}" aria-controls="collapse{{$count}}">
                        {{$question}}
                    </a>
                </h5>
            </div>
        </div>
    </div>
     <div id="collapse{{$count}}" class="collapse" aria-labelledby="heading{{$count}}" data-parent="#accordion">
                <div class="card-body bg-light">
                    {{$answer}}
                </div>
            </div>
        </div>
    @endforeach()

In case of the $counts variable the reason I am not using $key instead is that I wish to achieve One, Two, Three and not 1, 2, 3.

The code above is of course in correct. What would be the best way to achieve this? Is it even possible?

Waleed Ahmad
  • 57
  • 3
  • 12
  • You can't really do an `@foreach()` like that. You can do nested `@foreach` loops, or an `@for` loop, like `@for($i = 0; $i < count($questions); $i++)`, then `$questions[$i]` would be `question1`, and `$answers[$i]` would be `answer1`, etc. That works if all of your arrays are the same length, but I'm not 100% sure what you're trying to do. – Tim Lewis May 06 '20 at 16:24
  • Yes I wanted to do something like that and I tried a `@for` loop but got an error. If I were to use a `@for` loop how should the $faqs array look like? – Waleed Ahmad May 06 '20 at 16:27
  • 1
    Almost like you have now, but your array keys shouldn't be variables: `$questions => ['question1','question2','question3'],` should just be `'questions' => ...`: `$faqs = [ 'questions' => ['question1','question2','question3'], 'answers' => ['answer1','answer2','answer3'], 'counts' => ['One','Two','Three']];` (Can't really post block of code, but you get the idea. Also, don't mix syntaxes; use `array()` or `[]`, not both) – Tim Lewis May 06 '20 at 16:28
  • Oops the variable part was a mistake in the question I will edit that. And yes it was probably because of this array syntax error that I got an error before. I will try it and then let you know if it worked. – Waleed Ahmad May 06 '20 at 16:34
  • Does this answer your question? [Two arrays in foreach loop](https://stackoverflow.com/questions/4480803/two-arrays-in-foreach-loop) - Found this duplicate, seems to be the same thing you're running into, just in base PHP instead of Laravel. But the logic is the same :) – Tim Lewis May 06 '20 at 16:43
  • Yes your solution with for loop worked for me. Thank you so much. You can answer below so I can mark it as the right solution – Waleed Ahmad May 06 '20 at 16:53
  • It's been marked as a duplicate, and I agree with that actually. The approach for using one `foreach()`, with the `$index` variable is essentially the same as doing the `@for()` loop. I would go over to that question and give an upvote to one of the answers there :) – Tim Lewis May 06 '20 at 16:57

1 Answers1

0

you cant access your faq variable in your blade like this, due the wrong usage of with. you should do this:

return view('frontend.page.faq')->with(['faqs'=>$faq]);

first of all do this in your controller:

public function faq(){
    $faqs=array(
       'questions' => ['question1','question2','question3'],
       'answers' => ['answer1','answer2','answer3'],
       'counts' => ['One','Two','Three']
   );
  return view(frontend.page.faq')->with(['faqs'=>$faqs]);
}

then in your blade you can access them like so:

@foreach($faqs['questions'] as $question)
{{dd($question)}}

@endforeach

now you have your faqs in your blade

arya_la
  • 399
  • 2
  • 10
  • They're not trying to access `$faqs` in the view, they're access `$questions`, `$answers` and `$counts`, which, with the syntax you posted, are now all undefined. – Tim Lewis May 06 '20 at 16:25
  • After the edit, this still doesn't solve the problem. Yes, that's an approach, but the question is about how you do a `foreach` loop over multiple variables. This does not answer that question. How do you do a `foreach` over `$faqs['questions']`, `$faqs['answers']`, and `$faqs['counts']` at the same time? – Tim Lewis May 06 '20 at 16:34
  • @TimLewis well as long as i know i don't think it is possible – arya_la May 06 '20 at 16:56