Really easy to do with Form::model
{!! Form::model($user, ['method' => 'patch', 'route' => ['user.update', $user->id]]) !!}
// All fields will be filled in according to the model values
{!! Form::close() !!}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm populating an edit form for an existing model. Is there any way to load a form's old() values to match an existing model?
Really easy to do with Form::model
{!! Form::model($user, ['method' => 'patch', 'route' => ['user.update', $user->id]]) !!}
// All fields will be filled in according to the model values
{!! Form::close() !!}
I keep seeing information regarding the Form class. Is that an extension?
It's a package you can use, makes your life really easy working with forms. Everything you need can be found here: http://laravelcollective.com/docs/5.0/html
@bobbybouwmann I'm having a bit of difficulty with the route parameter for this.
My URI is as follows:
/{board}/post/{post}/mod/{action1?}/{action2?}/{action3?}
I have no idea how I'd put this in the route.dot.format. Is there any way to just specify the action directly?
I think you can pass in an array like this
{!! Form::model($user, ['method' => 'patch', 'route' => ['user.update', ['board' => $user->board, 'thread' => $user->thread]]]) !!}
Without the package, you can use the old() function.
@JeffreyWay my question was specifically how to populate the old function with data that was not sent previously. I want to make old('something') equal what I tell it to be.
You can put the array in "_old_input" variable, and use old() to get back.
session()->put('_old_input', []);
You can give it a default value like this:
old('name', $defaultValue);
old('name', $defaultValue);
@bobbybouwmann Works with your tip! Tks.
What I wanted was to set the old values from the controller, so I found this gist that was exactly what I was looking for:
// SET THE input old value with following values Input::old('key');
Session::set('_old_input.key', Input::get('value'));
EDIT: now using the global helper: session(['_old_input.key' => 'value'])
@gutierrezps In my case I had to do the following to test my blade component:
request()->setLaravelSession(session());
$view = $this->withSession(['_old_input.username' => 'john.doe'])
->blade('<input name="username" value="{{ old("username") }}"/>');
@gutierrezps that would have to be a really weird use case ?
@Snapey In my project, I want a form to be pre-filled with values the user has entered in the past, not bound to a specific model (so no Form::model).
Since I'm using Laravel Collective, I don't deal with the old() helper directly.
Setting the helpers' $value parameter would work, but it's a reasonable ammount of fields.
Can't believe this still hadn't been answered.
I finally found the answer myself. You use flash(), flashOnly() or flashExcept() to flash request values so they appear in old():
@Eggwink you're a hero :)
I'm reusing a form both for create and for edit
i'd like to avoid to test if old then old, otherwise if set the model->field show model-field.
Is there a way?
Yes, sure
In your create controller method, pass a new empty model to the view. eg the proverbial post example
public function create()
{
$post = new Post;
return view('post.edit', compact('post');
}
Now in the form view you can use old helper and fallback to the model if there is no old data
<input name="title" value="{{ old('title', $post->title) }}" />
now, the old function will show the latest entry (after a validation error) or the value from the model (if editing) or null from the new model (if creating)
The other thing I do is use exists to decide which <form> signature to use;
@if($post->exists)
<form method="POST" action="{{ route(post.edit,$post->id) }}">
@method('PATCH')
@else
<form method="POST" action="{{ route(post.create) }}">
@endif
@csrf
<input...... etc
Please or to participate in this conversation.