Home » Php » php – Laravel blade "old input or default variable"?

php – Laravel blade "old input or default variable"?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

How to&Answers:

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') }}"

Answer:

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.

Answer:

Try this:

<textarea name="alamat" id="" class="form-control" placeholder="Alamat">{{ old('alamat')[email protected]$biodata->alamat }}</textarea>