Answer the question
In order to leave comments, you need to log in
How to correctly specify where() when outputting in foreach?
Such situation. I display categories of clients. Accordingly, in the categories themselves, you need to display the clients corresponding to them.
From the controller I transfer these same clients, but how to display under each category of clients related to it?
Tried like this. Categories displays, but there are no customers.
@foreach($categories as $category)
<div class="slider-title clients-row">{{ $category['title'] }} <span class="plus">+</span></div>
<div class="slider-content">
<ul>
@foreach($clients->where('category', $category['id']) as $client)
<li><span>{{ $client['title'] }}</span></li>
@endforeach
</ul>
</div>
@endforeach
Answer the question
In order to leave comments, you need to log in
Make the Category model link to the Client model with the hasMany() method
@foreach($categories as $category)
<div class="slider-title clients-row">{{ $category['title'] }} <span class="plus">+</span></div>
<div class="slider-content">
<ul>
@foreach($category->clients as $client)
<li><span>{{ $client['title'] }}</span></li>
@endforeach
</ul>
</div>
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question