Jeton Thaçi Jeton Thaçi - 11 months ago 84
PHP Question

Laravel blade "old input or default variable"?

I want to show the old input in input value. If there isn't old input, than show other variable:

value="{{ old('salary_' . $employee->id) or 'Default' }}"

But when there is no old input, it gives me 1 instead of the default value!

I think the problem has something to do with the concatenation, but I don't know how to fix it!?

Answer Source

or is a comparison operator in PHP, so your code is evaluating to true, or 1. What you want is a ternary if statement.

As mentioned, or can be used in blade as shorthand for a ternary if statement.

But you can (and should) just pass the default value as the second argument to the function, like so:

value="{{ old('salary_' . $employee->id, 'Default') }}"