user1535268
user1535268

Reputation: 131

Laravel Blade Issue

I am upgrading a laravel site from 5.6 to 5.8. I have some forms where it will set the input value to something if the variable is available else it will leave it blank similar to below. This was working fine on 5.6 but when I try it with 5.8 for every instance of this it shows a value of 1. Even if I put something like or 'Joe' it still ends up putting 1 there. If I remove the or '' it will work just fine and display what is sent over for $data->name.

{{ $data->name or '' }} 

Just curious if anyone has seen this behavior as I have not. If I dd $data in the controller or the blade template the data is as expected and not just full of 1's.

Thanks

Upvotes: 1

Views: 246

Answers (2)

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Laravel or operator was changed in laravel 5.7 to ??.

The Blade "or" operator has been removed in favor of PHP's built-in ?? "null coalesce" operator, which has the same purpose and functionality

So {{ $data->name or '' }} is now {{ $data->name ?? '' }}

Upvotes: 1

Muhammad Dyas Yaskur
Muhammad Dyas Yaskur

Reputation: 8138

Your code $data->name or '' would return true or false, if $data->nameis true so the result is true, if $data->nameis false so the result is false. It's weird if on the old version your code was worked.

You should use ?:(ternary operator) or ??(null coalescing operator)

see detail in PHP ternary operator vs null coalescing operator

Upvotes: 0

Related Questions