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

cristian9509's avatar

Populate a form for editing/updating. How?

I have a form for storing/editing addresses. I am not using the Form facade but rather plain html. In case of failed validation the form inputs value = old('input_name'). I am trying to understand what is the best approach to populate the form for editing and then updating. So after the user stores their address, they may need to change it later on. I want to use the same view and populate the addresses and then update the record if validation fails!

0 likes
13 replies
opheliadesign's avatar

@cristian9509 Might want to give Laravel Collective's Forms & HTML package a try. It has the ability to use form model binding, it's my go-to for this sort of thing. http://laravelcollective.com/docs/5.0/html#form-model-binding

From their docs -

Often, you will want to populate a form based on the contents of a model. To do so, use the Form::model method:

echo Form::model($user, array('route' => array('user.update', $user->id)))

Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named email, the user model's email attribute would be set as the value. However, there's more! If there is an item in the Session flash data matching the input name, that will take precedence over the model's value. So, the priority looks like this:

  1. Session Flash Data (Old Input)
  2. Explicitly Passed Value
  3. Model Attribute Data
1 like
Snapey's avatar

At some point you will notice that you want a different form opening tag for creating a new record and updating an old record. I manage this by having a create view and an edit view and then a partial that has all the form fields so that you only have one form to maintain.

I'd post some code, but I also use Laravel Collective Form library.

1 like
slewis's avatar

This is the way I came up with using only plain HTML:

resources/views/users/form.blade.php

<form method="POST" action={{ $action or "" }}>
    @if( !empty($method) && in_array($method, ["PUT", "PATCH", "DELETE"]) )
        {{ method_field($method) }}
    @endif
    {{ csrf_field() }}
    <input label="name" type="text" name="name" value="{{ $user->name or old('name') }}"></input>
    <button type="submit" class="btn btn-primary">{{ $submit_text or "Submit" }}</button>
</form>

This takes advantage of blades 'or' statement, which checks if the variable exists.

In your edit or create views, include the form partial:

@include('users.form')

In the controller, define those variables:

Create

$action = route('users.store');
$method = "POST";
$submit_text = "Create";

 return view('users.create', compact('action', 'method', 'submit_text'));

Edit

$user = User::find($id);
$action = route('users.update', $user->id);
$method = "PUT";
$submit_text = "Update";

return view('users.edit', compact('user', 'action', 'method', 'submit_text'));

Note that we will probably switch to use the Laravel Collective Form library eventually, but this works for now.

2 likes
Snapey's avatar

@slewis a few tips

{{ $user->name or old('name') }}

Old will do the test for you.

{{ old('name', $user->name) }}

This is a better way around because when editing a form, first time around there is no old data so the data from the model is used. But then, if you get a validation error, the old value comes into play.

ah you say, when using the form to create a model (not edit one) then you get an error trying to get name from an unset object.

The way around this is to always pass your view a new model.

public function create()
{
    $user = new User;

    return view('users.edit, compact('user'))
}

Finally, you can test if it is a new model by asking it


@if($user->exists)
    <form method="POST" action="users">
@else
    <form method="POST" action="users/{{ $user->id }}">
    {{ hidden_field('patch')}}
@endif

11 likes
chuckd's avatar

@Snapey This is the nice clean approach I've been looking for as best practices are a little lacking in the docs. Just to note the conditional in the last snippet is flopped. Should be:

@if($user->exists)
    <form method="POST" action="users/{{ $user->id }}">
    {{ hidden_field('patch')}}
@else
    <form method="POST" action="users">
@endif 

I'm new to Laravel but have experience with other frameworks. Do you have any insight as to why the HTML helpers, like the ones found in Laravel Collective, have been removed in more recent versions of Laravel? Seem to be commonplace in other frameworks.

1 like
Snapey's avatar

@chuckiebgoode you may notice early in this thread i said that I was using the Forms package. I had not developed many sites in Laravel then, but not long after that I got fed up with wrestling with the package each time I wanted to do something out of the ordinary.

Sure it's possible to some extent to add extra form attributes but once you want control over the error styling (for instance) you start to get frustrated.

Once you realise how to do the model binding, the rest is easy.

My guess is that Taylor thought this package was too opinionated about the output styling and could see it as being a source of a lot of PRs .. too high maintenance

robashton's avatar

@snapey just following this thread and JW's 'Laravel 5.7 from Scratch' series. When trying to get the correct value into the input I think that value="{{ $project->title, old('title') }} is what I need. So when a user changes a title in the input, but then validation fails on the description field, the stored value of 'title' is over written rather than it using the modified (but not yet committed) 'title'. Am I using this correctly?

Snapey's avatar

@robashton

no, its

value="{{ old('title',$project->title) }}

You say, if there is an old value then present that. It the value is null then use the model value

1 like
babonday's avatar

where are the docs for crud in laracasts etc??? very common thing to want to do . Small things like snapey mentioned, using 'old' so that you dont have to create a fresh edit form...all these things are what newbies need tuts on to work out the 'simple' and time saving stuff.

Snapey's avatar

deprecated now isnt it?

what is?

Please or to participate in this conversation.