What is the best practice to show old value when editing a form in Laravel?

naneri picture naneri · Jul 19, 2016 · Viewed 41.8k times · Source

I am writing the logic for an edit form and have some complications when displaying data in the inputs.

When I initially show the form, I show the records values like:

value="{{$dog->title}}"

Then when the form does not pass the validation I need to show the old input, so that user does not loose what he has already input. So I need to have a way to display old data like:

value="{{old('title')}}"

Because I need to input old data in case it exists, I ended up with this code:

value="{{$dog->title or old('title')}}"

And in controller I check if Request has old input, I assign the $dog var a null value.

I wanted to ask if that is considered an OK practice or is there a better and 'correct' way to do it?

Answer

ikurcubic picture ikurcubic · Feb 2, 2017

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