1

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

user1535268
  • 121
  • 1
  • 2
  • 10
  • 1
    Don't know blade, but if it was pure PHP then I would suggest that you use the `??` operator: `echo $data->name ?? ''`, so maybe `{{ $data->name ?? '' }}` will work? – Alon Eitan Dec 15 '19 at 14:44
  • 1
    @AlonEitan that seemed to do it. Thanks. Weird the old way didn't work as it still shows in the docs for blade to use or but this is just as good. – user1535268 Dec 15 '19 at 14:57

2 Answers2

1

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 ?? '' }}

Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
0

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

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73