A
A
Alexander M2018-08-21 15:52:30
Template Engines
Alexander M, 2018-08-21 15:52:30

Make first letters of array delimiters in Blade Laravel?

Good afternoon. I wrote an almost working version in PHP, but somehow it looks ugly in the Blade template engine.
Maybe someone knows how to implement something similar in blade.
I want to divide the list of brands alphabetically. There are also names in Cyrillic.
An object is passed to the template.
which already contains data for each brand of car.
but how to display using standard means in Blade, in the form of
A
Acura
Audi
B
BMW
Bugatti
BYD
, etc.
I get the usual list like this.

@foreach($allMark as $mark)
    <li class="mark-list-li">
         <a href="/{{$mark->url}}/"><span class="name-li">{{$mark->name}}</span> ({{$mark->rus_name}})</a><span class="gray-color"> - {{$mark->models_count}} моделей</span>
     </li>
@endforeach

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anlamas, 2018-08-21
@alexander7779

https://laravel.com/docs/5.6/collections#method-groupby

$allMark = $allMark->groupBy(function ($item, $key) {
    return strtoupper(mb_substr($item->name, 0, 1));
});

@foreach($allMark as $key => $marks)
    {{ $key }}
    @for@foreach($marks as $mark)
        {{  $mark->name }}
    @endforeach
@endforeach

A
Antonio Solo, 2018-08-21
@solotony

standard blade will not work. you have to do php inserts. although the option that you suggested in the comments with two-level arrays is beautiful

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question