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.
@endif

It works and it’s ok to write it but Laravel has a way better solution:

@unless (Auth::check())
    You are not signed in.
@endunless

Another example – let’s say we have to show our posts, unless there are no posts:

@unless(count($posts) == 0)
    
{{ $post->title }}
@endunless

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.

Like our articles?
Check out our Laravel online courses!

2 COMMENTS

  1. Hi, please help,
    I am new to laravel, and i don’t know where even to start, or if i am asking the question correct…

    I have two tables: “students” and “users”.
    I use students for all students data.
    Now i want to allow these students to have access, so i use laravel auth to make users.
    I want when they login to have access only for records matching theyr email address.
    How can i do this to load a page where $students id is equal to user that have the same id as students?

    I don’t know where even to start.
    Thank you so much!
    ”’
    I tryed nothing as i don’t know where to start.
    ”’

LEAVE A REPLY

Please enter your comment!
Please enter your name here