Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jawsh's avatar

Is there a way to set old() values?

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?

0 likes
18 replies
bobbybouwmann's avatar

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() !!}
1 like
Jawsh's avatar

I keep seeing information regarding the Form class. Is that an extension?

Jawsh's avatar

@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?

bobbybouwmann's avatar

I think you can pass in an array like this

{!! Form::model($user, ['method' => 'patch', 'route' => ['user.update', ['board' => $user->board, 'thread' => $user->thread]]]) !!}
JeffreyWay's avatar

Without the package, you can use the old() function.

Jawsh's avatar

@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.

dipeshdwivedi's avatar

You can put the array in "_old_input" variable, and use old() to get back.

session()->put('_old_input', []);
2 likes
bobbybouwmann's avatar

You can give it a default value like this:

old('name', $defaultValue);
9 likes
gutierrezps's avatar

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'])

6 likes
arielenter's avatar

@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") }}"/>');
1 like
gutierrezps's avatar

@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.

1 like
realtebo's avatar

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?

Snapey's avatar

@realtebo

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

3 likes

Please or to participate in this conversation.