Begin by installing this package through Composer. Edit your project's composer.json
file to require laravelcollective/html
.
"require": {
"laravelcollective/html": "5.1.*"
}
Next, update Composer from the Terminal:
composer update
Next, add your new provider to the providers
array of config/app.php
:
'providers' => [
// ...
Collective\Html\HtmlServiceProvider::class,
// ...
],
Finally, add two class aliases to the aliases
array of config/app.php
:
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// ...
],
Looking to install this package in Lumen? First of all, making this package compatible with Lumen will require some core changes to Lumen, which we believe would dampen the effectiveness of having Lumen in the first place. Secondly, it is our belief that if you need this package in your application, then you should be using Laravel anyway.
{!! Form::open(['url' => 'foo/bar']) !!}
//
{!! Form::close() !!}
By default, a POST
method will be assumed; however, you are free to specify another method:
echo Form::open(['url' => 'foo/bar', 'method' => 'put'])
Note: Since HTML forms only support
POST
andGET
,PUT
andDELETE
methods will be spoofed by automatically adding a_method
hidden field to your form.
You may also open forms that point to named routes or controller actions:
echo Form::open(['route' => 'route.name'])
echo Form::open(['action' => 'Controller@method'])
You may pass in route parameters as well:
echo Form::open(['route' => ['route.name', $user->id]])
echo Form::open(['action' => ['Controller@method', $user->id]])
If your form is going to accept file uploads, add a files
option to your array:
echo Form::open(['url' => 'foo/bar', 'files' => true])
Laravel provides an easy method of protecting your application from cross-site request forgeries. First, a random token is placed in your user's session. If you use the Form::open
method with POST
, PUT
or DELETE
the CSRF token will be added to your forms as a hidden field automatically. Alternatively, if you wish to generate the HTML for the hidden CSRF field, you may use the token
method:
echo Form::token();
Route::post('profile',
[
'before' => 'csrf',
function()
{
//
}
]
);
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, ['route' => ['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:
This allows you to quickly build forms that not only bind to model values, but easily re-populate if there is a validation error on the server!
Note: When using
Form::model
, be sure to close your form withForm::close
!
echo Form::label('email', 'E-Mail Address');
echo Form::label('email', 'E-Mail Address', ['class' => 'awesome']);
Note: After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well.
echo Form::text('username');
echo Form::text('email', '[email protected]');
Note: The hidden and textarea methods have the same signature as the text method.
echo Form::password('password', ['class' => 'awesome']);
echo Form::email($name, $value = null, $attributes = []);
echo Form::file($name, $attributes = []);
echo Form::checkbox('name', 'value');
echo Form::radio('name', 'value');
echo Form::checkbox('name', 'value', true);
echo Form::radio('name', 'value', true);
echo Form::number('name', 'value');
echo Form::date('name', \Carbon\Carbon::now());
echo Form::file('image');
Note: The form must have been opened with the
files
option set totrue
.
echo Form::select('size', ['L' => 'Large', 'S' => 'Small']);
echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], 'S');
This will create an <option>
element with no value as the very first option of your drop-down.
echo Form::select('size', ['L' => 'Large', 'S' => 'Small'], null, ['placeholder' => 'Pick a size...']);
echo Form::select('animal',[
'Cats' => ['leopard' => 'Leopard'],
'Dogs' => ['spaniel' => 'Spaniel'],
]);
echo Form::selectRange('number', 10, 20);
echo Form::selectMonth('month');
echo Form::submit('Click Me!');
Note: Need to create a button element? Try the button method. It has the same signature as submit.
It's easy to define your own custom Form class helpers called "macros". Here's how it works. First, simply register the macro with a given name and a Closure:
Form::macro('myField', function()
{
return '<input type="awesome">';
});
Now you can call your macro using its name:
echo Form::myField();
Generate a HTML link to the given URL.
echo link_to('foo/bar', $title = null, $attributes = [], $secure = null);
Generate a HTML link to the given asset.
echo link_to_asset('foo/bar.zip', $title = null, $attributes = [], $secure = null);
Generate a HTML link to the given named route.
echo link_to_route('route.name', $title = null, $parameters = [], $attributes = []);
Generate a HTML link to the given controller action.
echo link_to_action('HomeController@getIndex', $title = null, $parameters = [], $attributes = []);