I
I
igor11122018-10-08 14:16:44
Laravel
igor1112, 2018-10-08 14:16:44

Blade pattern mess?

Hello. In the site menu, I need to display seven categories, and then add a "More" button, and when you hover over it, the rest of the categories will appear.
I started like this:

@if($categories)
    @for ($i = 0; $i < count($categories); $i++)
        <li><a href="{{ url('/category') . '/' . $categories[$i]['id'] }}">{{ $categories[$i]['name'] }}</a></li>
        <?php
            if($i == 6) {
                $categories = array_splice($categories, 7);
                break;
            }; 
         ?>
     @endfor
@endif

This is very ugly, and in my opinion this should not be in the template. How smart programmers resolve such incidents. To make similar calculations in the controler? There are several such places in the template. Well, ok, then the controller will be clogged with this mess. Tell me plz.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2018-10-08
@alexey-m-ukolov

View Composers are used to separate logic from templates without cluttering up controllers .

V
Vitaly Yushkevich, 2018-10-08
@yushkevichv

Check out this package
https://github.com/spatie/laravel-menu

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question