Matt Larsuma
Matt Larsuma

Reputation: 1519

Laravel 5.4/Blade not showing a variable that is an empty string with a space: " "

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

Answers (3)

Script47
Script47

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

Matt Larsuma
Matt Larsuma

Reputation: 1519

empty(trim($customer->PhoneWork)) does the trick! It checks if the trimmed string is empty.

Upvotes: 0

Kart Av1k
Kart Av1k

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

Related Questions