Answer the question
In order to leave comments, you need to log in
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. ???
// контроллер
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question