Using your value field for both create and edit forms.

Submitted by laraning - 3 months ago

We don't like to duplicate a form for the "create" and "edit" methods. But how can you then put a value using the old and at the same time using the model attribute value without raising an exception?

In your form, you would write:

<input name='title' value="{{ old('title', $model->title) }}">

That would throw an exception in case your $model is not populated. As example,
when you are in a form "create" action.

So, to avoid that error and be able to use this form in both update and create
action, you should write:

<input name='title' value="{{ old('title', optional($model)->title) }}">

The optional helper allows you to control the null exception without throwing it
due to the invalid property in a null object.

Hope it helps!

Bruno Falc√£o
www.laraning.com
comments powered by Disqus