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

Romain's avatar
Level 30

If/else at the end of a blade file throw exception

Hello,

Blade is throwing an exception when I try having an if/else statement at the end of a file for closing a specific tag. Would anyone know why?

@if (auth()->user())
    </x-app-layout >
@else
    </x-guest-layout>
@endif

I get: syntax error, unexpected token "else"

I have the same if/else at the top of the file for opening the proper tag, and it works.

0 likes
10 replies
tykus's avatar

Trying removing the space:

</x-app-layout > <!-- here -->
</x-app-layout>
tykus's avatar

@Romain is that the offending code - can you get past the error message if it was removed (albeit the closing tag is missing obviously)?

Sinnbeck's avatar

Can you show the complete file? I just tested it, and it seems to work fine here.

Romain's avatar
Level 30

@tykus that's the end of the file. It's the newest addition to this file. At the top I have the same code for the opening tags:

@if (auth()->user())
<x-app-layout>
@else
<x-guest-layout>
@endif

And that doesn't throw an error

Sinnbeck's avatar

@Romain So if you only have this, it still fails? Or is the error somewhere between the two

@if (auth()->user())
<x-app-layout>
@else
<x-guest-layout>
@endif

lalala

@if (auth()->user())
    </x-app-layou >
@else
    </x-guest-layout>
@endif
tykus's avatar

@Romain the problem here is the fact that you have Blade component tags inside the conditional blocks. You could instead use the @component Blade directive:

@if (auth()->user())
    @component('components.app-layout')
@else
    @component('components.guest-layout')
@endif

<!-- Rest of document -->

@endcomponent
Romain's avatar
Level 30

@tykus hmm if that's really the issue then I will rethink this logic. I'll try something else and get back to you guys

MohamedTammam's avatar

You're adding the component closing tag without the opening tag.

@if (auth()->user())
<x-app-layout ></x-app-layout >
@else
<x-guest-layout></x-guest-layout>
@endif

OR

@if (auth()->user())
<x-app-layout />
@else
<x-guest-layout />
@endif

Please or to participate in this conversation.