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).
Check if not null: whereNotNullSELECT * FROM users WHERE last_name IS NOT NULL; The equivalent to the IS NOT NULL condition in Laravel Eloquent is the whereNotNull method, which allows you to verify if a specific column's value is not NULL .
You can use the @isset blade directive to check whether the variable is set or not.
try this:
@if ($value !== '') {{ HTML::link($value,'some text') }} @endif
I prefer the @unless
directive for readability in this circumstance.
@unless ( empty($school->website) ) <a href="{{ $school->website }}" target="_blank">{{ $school->website }}</a> @endunless
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With