Laravel 4.2 blade: check if empty
Asked Answered
O

7

25

In Laravel blade you can do:

{{ $variable or 'default' }}

This will check if a variable is set or not. I get some data from the database, and those variables are always set, so I can not use this method.

I am searching for a shorthand 'blade' function for doing this:

{{ ($variable != '' ? $variable : '') }}

It is hard to use this piece or code for doing this beacuse of, I do not know how to do it with a link or something like this:

<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>

I tried:

{{ ($school->website != '' ? '<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>' : '') }}

But, it does not work. And, I would like to keep my code as short as possible ;)

Can someone explain it to me?

UPDATE

I do not use a foreach because of, I get a single object (one school) from the database. I passed it from my controller to my view with:

 $school = School::find($id);
 return View::make('school.show')->with('school', $school);

So, I do not want to make an @if($value != ''){} around each $variable (like $school->name).

Och answered 15/12, 2014 at 14:23 Comment(0)
M
22

try this:

@if ($value !== '')
    {{ HTML::link($value,'some text') }}
@endif
Marlyn answered 15/12, 2014 at 14:28 Comment(8)
Indeed, that is a solution instead of typing <a href=... but, I do not want an if around each line of my code. I prefer a shorthand solution, so my code will be easier to read.Och
@Och As you told above,your solution would not worked.At least mine works :) But I agree with you about being shorthandMarlyn
@Och I suggest you to edit your question and tell me how many variables you have in your view.maybe with a foreach() your problem will be solvedMarlyn
@Och You can write the if on one line without a problem @if($value != '') {{ HTML::link($value, 'some text') }} @endifDenyse
@lukasgeiter, I am going to use your method. And for the values without <a href.. I will use {{ ($value != '' ? $value : '') }}. Thanks!Och
@Och Why even do this? {{ ($variable != '' ? $variable : '') }} this means if the $variable is '' print out ''. Why don't just print it like it is: {{ $variable }}Denyse
@Denyse You're right! I am sorry, I mean this {{ ($variable != '' ? $variable : '-') }} (with a -) ;)Och
@Och Ahhhh! yeah that makes more sense :)Denyse
B
15

I prefer the @unless directive for readability in this circumstance.

@unless ( empty($school->website) )
    <a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>
@endunless
Bossuet answered 26/1, 2017 at 20:3 Comment(0)
M
9

With php 7, you can use null coalescing operator. This is a shorthand for @m0z4rt's answer.

{{ $variable ?? 'default' }}
Maroon answered 9/7, 2018 at 6:40 Comment(0)
S
6
{{ ($school->website != '' ? '<a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a>' : '') }}

change to

{{ ($school->website != '') ? '<a href="' . $school->website . '" target="_blank">' .  $school->website . '</a>' : '' }}

or the same code

{{ ($school->website != '') ? "<a href='$school->website' target='_blank'>$school->website</a>" : '' }}
Streptokinase answered 15/12, 2014 at 18:15 Comment(0)
C
2
{{ isset($variable) ? $variable : 'default' }}
Cathodoluminescence answered 12/1, 2017 at 9:11 Comment(0)
B
1

I wonder why nobody talked about $variable->isEmpty() it looks more better than other. Can be used like:

@if($var->isEmpty())
    Do this  
@else
    Do that  
@endif
Bunton answered 9/4, 2019 at 8:53 Comment(0)
K
0

From Laravel 5.4, you can also use the @isset directive.

@isset($variable)
    {{-- your code --}}
@endisset

https://laravel.com/docs/9.x/blade#if-statements

Kyliekylila answered 3/3, 2022 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.