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!?
The old helper function is used to retrieve an old input item. It is a shortcut to calling the "Illuminate\Http\Request::old" instance method. It accepts a $key argument and a $default argument.
You can use the @isset blade directive to check whether the variable is set or not.
In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.
Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.
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') }}"
You can use the code (for PHP 7):
{{ old('field_name') ?? $model->field_name ?? 'default' }}
For checkbox checked attribute use the code (if default value is false):
{{ (old() ? old('field_name', false) : $model->field_name ?? false) ? 'checked' : '' }}
Construction {{ $something or 'default'}}
works only for variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With