Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

check202's avatar

[SOLVED] Using Blade @if , @elseif directives - some advice needed

Hi folks,

Hoping I can get some advice on how to use the @if and @elseif directives within Blade or if there is a better way of implementing what I'm trying to do.

The scenario: I have about 100 menu navigations items that depending on their "Type" (A value kept in the DB) will need to be wrapped with certain row elements plus their own HTML tags.

So for example:

If it's Type is "1" it would be a Main Header and presented like so:

 <li class="{{  $navigation->classL  }}">{{  $navigation->main_Header  }}</li>

If it's Type is "2" it would be a Title and presented like so:

 <li class="treeview">
            <a href="{{ $navigation->link  }}">
                <i class="{{ $navigation->classL  }}"></i> <span>{{ $navigation->title }}</span>
                <i class="{{ $navigation->classR  }}"></i>
            </a>  

If it's Type is "3" it would be a Menu Item and presented like so:

<ul class="treeview-menu">
                <li><a href="{{ $navigation->link }}"><i class="{{ $navigation->classR  }}"></i>{{ $navigation->title }}</a></li>
</ul>
0 likes
5 replies
check202's avatar

Well yes but getting it working is another feat. I keep getting parse error which translates to me as "I'm a fool and made a spelling / syntax mistake/error"

Here is a basic nut shell of what I got. Just keeping it nice and simple before binding into models etc.

The router:

  • Just points to said method on controller to load as it instructs. No point including here.

The Controller:

public function index()
    {
        $navigations = DB::table('Navigation')->get();

        return View('navigation.index')->with('navigations', $navigations);
    }

The view File

@section('content')
@if (isset($navigations)) 

      @foreach ($navigations as $navigation)

        @if($navigation->type == 1)
         <li class="{{ $navigation->classL }}">{{  $navigation->main_Header }}</li>
        @elseif($navigation->type == 2)
         <li class="treeview">
            <a href="{{ $navigation->link }}">
                <i class="{{ $navigation->classL }}"></i> <span>{{ $navigation->title }}</span>
                <i class="{{ $navigation->classR }}"></i>
            </a>
        </li>
        @elseif($navigation->type == 3)
        <ul class="treeview-menu">
             <li><a href="{{ $navigation->link }}"><i class="{{ $navigation->classR }}"></i>{{ $navigation->title }}</a></li>
        </ul>
    @endif
@endsection
d3xt3r's avatar
d3xt3r
Best Answer
Level 29

Use a good ide, even just copy pasting view code showed me to errors regarding directives not being closed, first if and foreach.

1 like
check202's avatar
  • Face palm*

Got it to work. Thanks heaps for the help folks. :)

Note to self: PHPstorm is better than Sublime in pointing out Syntax errors.

Please or to participate in this conversation.