G
G
GaserV2016-04-11 16:11:45
Laravel
GaserV, 2016-04-11 16:11:45

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

1 answer(s)
S
Stanislav Pochepko, 2016-04-11
@DJZT

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 question

Ask a Question

731 491 924 answers to any question