Laravel blade "old input or default variable"?
Asked Answered
T

6

50

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!?

Tympanites answered 11/11, 2015 at 14:36 Comment(0)
L
84

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') }}"
Leverick answered 11/11, 2015 at 14:48 Comment(7)
Now here's the fun thing; using {{ $value or $default }} is a short-hand way of doing a ternary statement in Blade (Reference). Therefore, I'm not certain that this is right. It is, however, clearer!Clavichord
As in Laravel docs: {{ isset($name) ? $name : 'Default' }} shorthand way {{ $name or 'Default' }}Ineluctable
@ChrisForrence you are correct! you learn something every day... the better way to accomplish this is by passing the default value as the second argument.Leverick
This doesn't solve the problem. When the old input is empty, it gives nothing ... the second variable is not echoed.Constriction
Actually it has solved the problem, I was closing the brackets before the second variable, thanks guys.Constriction
Thanks! This was exactly what I was looking for. I sure wish the Laravel Docs had more tutorials, but I guess that's the reason for the Laracasts.Sapotaceous
for php 7+ value="{{ old('salary_' . $employee->id ?? 'Default') }}"Bias
T
16

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

Temikatemp answered 27/5, 2020 at 12:57 Comment(1)
For checked you can use <input type="checkbox" name="active" value="active" @checked(old('active', $user->active)) />Surmise
S
3

As described in the Laravel doc: "If you are displaying old input within a Blade template, it is more convenient to use the old helper:". So if you need add/edit data form (when you need to use edit form for add and edit in edit mode you need to use loaded data from model (database)) to show values from the model (through controller) you can use following:

name="some_value" value="{{ $some_value or old('some_value', $the_value) }}"

where is "some_value_from_model" variable name in view array. In this case it should be at first $some_value will be used to set in the "value" and, if no, it will try to use old (value from request from name "some_value") and if not old then '' should be used.

Thanks WoodyDRN for comment.

Shupe answered 28/10, 2016 at 10:41 Comment(1)
But if you do it this way $some_value or old('some_value') - then if the user enters anything in the input fields, and submits, and there was an error, then it would default back to $some_value and deleting whatever they wrote. old('some_value', $some_value) would fix that.Traumatism
C
1

Try this:

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

Explanation:

If an old value is there, it will be set, otherwise, 'Default' will be set as value.

Corolla answered 19/12, 2022 at 7:18 Comment(0)
T
-6

Try this:

<textarea name="alamat" id="" class="form-control" placeholder="Alamat">{{ old('alamat').@$biodata->alamat }}</textarea>
Trumpet answered 21/6, 2018 at 1:17 Comment(2)
A more detailed explanation is needed.Medrek
What is this actuallyTibbetts

© 2022 - 2024 — McMap. All rights reserved.