2

I tried to use two default value for a input with condition in blade:

<input type="text" class="form-control" id="wallet" name="wallet" value="{{old('wallet') or 0}}">

I want the old() to be use when the inputs value is incorrect and the other should be used when the page is open for the first time and for is not submitted yet, but when i run the code the 0 as default value is not set in input.Where is the problem?

1 Answer 1

11

old() can have a second parameter for default value. So you can use, old('wallet', 0).

Sign up to request clarification or add additional context in comments.

3 Comments

This works fine for if you just have two potential parameters so is fine for this use case however if OP wanted to use more than one potential option eg. if editing a model, you could null coalesce (??) operator like so : old('value') ?? $model->value ?? $defaultValue
@KyleWardle why not old('value', $model->value ?? $defaultValue) ?
@Robert That is true actually, I guess the only situation where that would not be quite enough would be if you wanted the $model->value to take priority over the old value as with this current method it will always prioritise the old('value')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.