所以我想做的是创建一个FAQ手风琴,但是使用@foreach循环而不复制和粘贴整个列表。我的控制器代码如下:
public function faq(){
$faqs=array(
'questions' => ['question1','question2','question3'],
'answers' => ['answer1','answer2','answer3'],
'counts' => ['One','Two','Three']
);
return view('frontend.pages.faq')->with($faqs);
}
这是我希望实现的目标:
@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()
在使用$ counts变量的情况下,我不使用$ key的原因是我希望获得1、2、3而不是1、2、3。
上面的代码当然是正确的。实现这一目标的最佳方法是什么?甚至有可能吗?
答案 0 :(得分:-1)
由于with的错误使用,您无法像这样访问刀片服务器中的faq变量。您应该这样做:
return view('frontend.page.faq')->with(['faqs'=>$faq]);
首先在您的控制器中执行此操作:
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]);
}
然后在刀片中您可以像这样访问它们:
@foreach($faqs['questions'] as $question)
{{dd($question)}}
@endforeach
现在您的常见问题解答了