Blade tip: @unless instead of @if-statement

Another quick tip for you guys from Laravel world - this time it's about Blade template engine. We have all been in a situation where we had to write something like this:
@if (!Auth::check())
    Please log in.
It works and it's ok to write it but Laravel has a way better solution:
@unless (Auth::check())
    You are not signed in.
Another example - let's say we have to show our posts, unless there are no posts:
@unless(count($posts) == 0)
{{ $post->title }}
The command @unless checks if our expression returns FALSE - then shows the following data. If the expression returns TRUE - it will ignore the inner part.

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 52 courses (952 lessons, total 46 h 42 min)
  • 77 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials