This tutorial is out of date and no longer maintained.
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.
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.
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.
// 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.
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!
{{ 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() }}
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.
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!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.