Tutorial

Laravel Form Model Binding

Draft updated on Invalid Date
Default avatar

By Chris on Code

Laravel Form Model Binding

This tutorial is out of date and no longer maintained.

Introduction

Laravel is an extremely fun PHP framework that offers many convenient tools.

For most web applications, a person using Laravel will usually build the basic CRUD functionality for various resources. We’ve written about the CRUD part of Laravel before.

In this quick tip, we’ll be talking Laravel Form Model Binding, which helps the UPDATE portion of our applications.

What is Form Model Binding?

Normally, when we create an edit form, you would pull in the data of the item you want to edit. Then you would use that to populate your form’s input fields.

With Form Model Binding, your edit forms are automatically populated based on the item you send to the view using Laravel.

Let’s Demonstrate

First, we need to send data to our edit form. To do that, let’s set up our route. Since this is just a demonstration, we’ll just say we have an imaginary model already set up called Shark.

Our Shark will have 3 fields in our database.

  • id
  • name
  • email

Form Route

routes.php
        // route to show our edit form
        Route::get('shark/edit/{id}', array('as' => 'shark.edit', function($id)
        {
            // return our view and Shark information
            return View::make('shark-edit') // pulls app/views/shark-edit.blade.php
                ->with('shark', Shark::find($id));
        }));

        // route to process the form
        Route::post('shark/edit', function() {
            // process our form
        });

Now we have our route to show our Shark. There are many different ways you could set up this route. You can use resource controllers or just create the route yourself (like we did above).

That’s it for our route. Now that we have sent our Shark to our edit view, let’s go look at how Form Model Binding works.

Form Edit View

Normally, to create a form using Laravel, you would use echo Form::open() or if you are using the Laravel Blade templating engine, you would use {{ Form::open() }}.

When using Form Model Binding, you just have to make one very small adjustment: use {{ Form::model() }}. With that one adjustment, our form will automatically be populated!

app/views/shark-edit.blade.php
        {{ Form::model($shark, array('route' => 'shark.edit', $shark->id)) }}

            <!-- name -->
            {{ Form::label('name', 'Name') }}
            {{ Form::text('name') }}

            <!-- email -->
            {{ Form::label('email', 'Email') }}
            {{ Form::email('email') }}

            {{ Form::submit('Update Shark!') }}

        {{ Form::close() }}

Clean and Simple

Laravel brings in great features and this is definitely one of them. Just like that, you are able to bind data from a model to a form and populate the form easily.

If you have any further questions, take a look at the Laravel Docs on Form Model Binding.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Chris on Code

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel