K
K
kibo-132020-06-04 21:22:03
Laravel
kibo-13, 2020-06-04 21:22:03

Grouping identical records in a view (template)?

Good day to all.
Tell me how you can group duplicate entries in the "List of addresses" column so that the entries are displayed in the format: Street d.1, d.2, etc. ???

5ed93b1f6c446272510055.jpeg

// контроллер 
public function index()
{   
   $plots = Plot::get();
   return view('pages.plots.index', compact('plots'));
}
 
// вывод записей в шаблоне:
<thead class="thead-light">
 <tr>
  <th scope="col">#</th>
  <th scope="col">Участок</th>
  <th scope="col">Список адресов</th>
 </tr>
</thead>
<tbody>
 @foreach($plots as $id => $plot)
 <tr>
  <td>{{ $id+=1 }}</td>
  <td>{{ $plot->branch->name }}</td>
  <td>
   @foreach($plot->addresses as $address)
     {{ $address->street->name }} 
     {{ $address->num_home }}, 
   @endforeach
  </td>
  @endforeach
</tbody>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kibo-13, 2020-06-05
@kibo-13

Got the desired result by fixing the loop in the view:

@foreach($streets as $street) 
  @if($plot->addresses->where('street_id', $street->id)->count())
    {{ $street->name }}
  @endif

  @foreach($plot->addresses as $address) 
    @if($street->id == $address->street_id)
      д.{{ $address->num_home }}, 
    @endif
    
  @endforeach

  @if($plot->addresses->where('street_id', $street->id)->count())
    <br>
  @endif

@endforeach

5ed9d8f1c43d1081349979.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question