Laravel 4.2 blade: check if empty

Marten picture Marten · Dec 15, 2014 · Viewed 46.8k times · Source

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

Answer

soroush gholamzadeh picture soroush gholamzadeh · Dec 15, 2014

try this:

@if ($value !== '')
    {{ HTML::link($value,'some text') }}
@endif