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

laravel blade variable
laravel foreach index
laravel blade components
laravel blade tutorial
laravel blade directives
for loop in laravel blade file
how to write php code in laravel blade
laravel blade component example

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?

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

Blade Templates - Laravel, Unlike other popular PHP templating engines, Blade does not restrict you from You are not limited to displaying the contents of the variables passed to the view. the @isset and @empty directives may be used as convenient shortcuts for their Blade component tags start with the string x- followed by the kebab case  0 Laravel 5.4/Blade not showing a variable that is an empty string with a space: “ ” Jun 7 '18 0 PHP / Extract navigation from Array Aug 21 '18 0 How to insert a PHP variable into an SQL query Oct 1 '18

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)

Blade strips spaces inside variable, instead of showing 05/15 (5 spaces) 20/11/15 (10 spaces) 1.789,00 HTML isn't 'space sensitive' - so putting < space>< space>< space> will  Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.

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

trim - Manual, This function returns a string with whitespace stripped from the beginning and end of str . It may be useful to know that trim() returns an empty string when the argument is an unset/null variable. php version 5.4.27 there should be no spaces at the beginning and end of your echo statement, else trim will not work as  A common problem while working in a team is some custom variables needed, when only one person on the team knows that they are needed, and then other people have errors because they don't have that variable. Like API Tokens for 3rd party apps, default values for some function - basically, anything that should be in .env file. So here's an instruction for you, how to put new environment

empty - Manual, A variable is considered empty if it does not exist or if its value equals FALSE . empty() does not generate a warning if the No warning is generated if the variable does not exist. PHP 5.4 changes how empty() behaves when passed string offsets. Trim also takes care of other white space like line breaks and tabs. Become a Laravel Partner. Laravel Partners are elite shops providing top-notch Laravel development and consulting. Each of our partners can help you craft a beautiful, well-architected project.

[Request] Blade syntax for switch case · Issue #1857 · laravel , I love to make use of the blade syntax and the control statements on blade are clean and simple. GitHub is home to over 40 million developers working together to @switch($variable) @case(value) // Implementation goes here break the case is not captured) and why capture blank spaces if you allow  Stop Laravel Validation replacing underscores with a space in :attribute placeholder I am using a Validator to validate and return helpful messages to the users of a public API. Because these messages are to be consumed by developers I want them to be technical and specific.

[BUG] [5.5] Component slot returns HtmlString, doesn't handle empty , @component('test') // Not actually passing in null, this is a Add an empty / isset / null check on one of the slot variables; Pass that slot a null value; Watch the magic HtmlString, doesn't handle empty checks or isset properly [BUG] [5.4] There is also a package called laravel-blade-directives, which  Moreover, print_r() function has another optional parameter with true/false values – you can not only echo the variable, but return it as string into another variable. Then you can combine several variables into one and maybe log it somewhere, for example. So, in cases like this, dd() is

Comments
  • If you have space in a value - it is not empty.
  • That still returns false. There's ctype_space(), but I shouldn't rely on it always being a white space issue.
  • The value is currently a string with one space: " " The logic of !empty(trim($customer->PhoneWork)) makes sense, but it's not executing properly in the blade template. It's still returning false, when the current goal is to establish that it's true for the @unless.
  • If you continue to use unless, you'll need to get rid of the ! (not).
  • Yep! !empty(trim($customer->PhoneHome)) returns true and that currently has a phone number string. It then doesn't show :/
  • But of course! It just needs to be empty(trim($customer->PhoneWork)). Then it's checking if trim($customer->PhoneWork) is empty. That is true.
  • OP has mentioned that they don't want to rely on ctype_space.