Reputation: 1519
This is a weird question, as it feels so simple, but alas it has me stuck. I have a variable: $customer->PhoneWork
that is currently empty, returning " "
. In the Blade template, I want to conditionally render it only if it is not empty:
@unless (empty($customer->PhoneWork))
Work: {{ $customer->PhoneWork }}
@endunless
The problem is, though it seems like it would be empty right now, empty($customer->PhoneWork)
is false. I also tried is_null()
and !isset()
and all are experiencing the same issue. What am I missing here?
Upvotes: 1
Views: 457
Reputation: 14540
As per the documentation:
Determine whether a variable is empty
So if the variable is empty
it will return true
and if it is not, it will return false
.
As a result, you need to trim
the value and reverse the check:
@if (!empty(trim($customer->PhoneWork))
This check is now saying: If $customer->PhoneWork
(trimmed) is NOT empty
Upvotes: 2
Reputation: 1519
empty(trim($customer->PhoneWork))
does the trick! It checks if the trimmed string is empty.
Upvotes: 0
Reputation: 137
If I understand correctly, you need to perform an action if the string contains empty characters. so...
function empty($var)
is equal to !isset($var) || $var == false
.
You may need to use ctype_space($str)
Upvotes: 0