Laravel Form Model Binding Generates Empty Input Fields

If you use the HTML package from LaravelCollective to generate your Laravel forms, you may encounter a problem, when you use form model binding. It may occur, that your input fields remain empty.

The reason may be, that you’ve copied your creation form to your edit form and copied an empty string as default parameter.

Creation Form

{!! Form::open(['route' => 'my_route']) !!}
[...]
{{ Form::text('title', '', ['class' => 'text-field'])  }}

Edit Form

{!! Form::model($post, ['route' => ['my_route', $post]]) !!}
[...]
{{ Form::text('title', '', ['class' => 'text-field'])  }}

Because your second parameter for Form::text is an empty string, the correct value will be overwritten. Instead of the empty string, you have to change your parameter to null, so that Form::model can set the correct value:

{!! Form::model($post, ['route' => ['my_route', $post]]) !!}
[...]
{{ Form::text('title', null, ['class' => 'text-field'])  }}

Leave a Comment