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).

7 Answers 7

22

try this:

@if ($value !== '')
    {{ HTML::link($value,'some text') }}
@endif
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.
    – Marten
    Dec 15, 2014 at 14:34
  • 1
    @Marten As you told above,your solution would not worked.At least mine works :) But I agree with you about being shorthand Dec 15, 2014 at 14:37
  • @Marten 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 solved Dec 15, 2014 at 14:39
  • @Marten You can write the if on one line without a problem @if($value != '') {{ HTML::link($value, 'some text') }} @endif Dec 15, 2014 at 14:43
  • @lukasgeiter, I am going to use your method. And for the values without <a href.. I will use {{ ($value != '' ? $value : '') }}. Thanks!
    – Marten
    Dec 15, 2014 at 14:57
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
9

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

{{ $variable ?? 'default' }}
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>" : '' }}
2
{{ isset($variable) ? $variable : 'default' }}
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.