What is the best practice to show old value when editing a form in Laravel?
Function old have default parameter if no old data found in session.
function old($key = null, $default = null)
You can replace expression in template with
value="{{old('title', $dog->title)}}"
I know this has already been answered but I thought I would leave a little snippet here for others in the future.
Setting old value on input as @ikurcubic posted can be used the same way on radio button or select:
<input type="text" name="name" value="{{ old('name', $DB->default-value) }}" />
Select option:
<option value="Jeff" {{ old('name', $DB->default-value) == 'Jeff' ? 'selected' : '' }}>Jeff</option>
Radio Button:
<input type="radio" name="gender" value="M" {{ old('name', $DB->default-value)== "M" ? 'checked' : '' }} />
Another way of doing it; write a small if statement to determine which value should be evaluated:
@php
if(old('name') !== null){
$option = old('name');
}
else{ $option = $database->value; }
@endphp
<select name="name">
<option value="Bob" {{ $option == 'Bob' ? 'selected' : '' }}>Bob</option>
<option value="Jeff" {{ $option == 'Jeff' ? 'selected' : '' }}>Jeff</option>
</select>
<input type="radio" name="gender" value="M" {{ $option == "M" ? 'checked' : '' }} />
<input type="radio" name="gender" value="F" {{ $option == "F" ? 'checked' : '' }} />
Setting old value of input with an array name e.g name="name[]":
<input type="text" name="name[]" value="{{ old('name.0) }}" />
This will give you the old value of the input with an index of 0.
I have tested this and it works.