Opening a Form in Laravel

In this post, we will see how we can create an HTML <form> element using Laravel HTML and Form builder package.

Useful Read: How to Install Illuminate/HTML In Laravel 5?

To open a Form use the following code,

{{ Form::open() }}

The above code will generate following HTML output,

<form method="POST" action="http://yourcurrenturl.com" accept-charset="UTF-8">
<input name="_token" type="hidden" value="some_random_string">

By default, method is set to POST  and action consist of your current URL.

Form Pointing to a particular URL:

You can pass a URL parameter to which the form will point by passing URL in form of an associative array.

{{ Form::open(array('url' => 'http://example.com/foobar/')) }}

This code will produce following HTML output,

<form method="POST" action="http://example.com/foobar/" accept-charset="UTF-8">
<input name="_token" type="hidden" value="some_random_string">

As you can see, the URL will appear as the value of action attribute.

Form Pointing to a named route:

You can also use named route to populate the action attribute

{{ Form::open(array('route' => 'named.route')) }}

This will create the following HTML code

<form method="POST" action="http://example.com/foobar/" accept-charset="UTF-8">
<input name="_token" type="hidden" value="some_random_string">

Form Pointing to a Controller Action:

Also, you can provide controller action as parameter to action attribute within the <form> element.

{{ Form::open(array('action' => 'Controller@method')) }}

This will produce following output,

<form method="POST" action="http://example.com/foobar/" accept-charset="UTF-8">
<input name="_token" type="hidden" value="some_random_string">

action attribute will be populated with URL which will call the specified controller.

You can also pass parameters along with route as an array.

{{ Form::open(array('route' => array('route.name', $user->id))) }}
{{ Form::open(array('action' => array('Controller@method', $user->id))) }}

Enable File Upload:

If your going to allow users to upload file using forms, you need to enable file upload by passing ‘files’ => true along with the other parameters of the array.

{{ Form::open(array('url' => 'http://example.com/foobar', 'files' => true)) }}

The above code will add a enctype=”multipart/form-data” attribute to the form element.

<form method="POST" action="http://example.com/foobar/" accept-charset="UTF-8"
  enctype="multipart/form-data"><input name="_token" type="hidden" value="some_random_string">

Set Different method:

You can change the default POST method in your forms by passing method parameter along with the other parameters of the array.

{{ Form::open(array('url' => 'http://example.com/foobar', 'method' => 'put')) }}

This will generate following HTML code,

<form method="GET" action="http://example.com/foobar" accept-charset="UTF-8">

You can use GET, PUT, PATCH, POST and DELETE methods.

Closing The Form:

To close the form, use the following code. It will generate a </form> element.

{{ Form::close() }}

 

0 Comments

Leave a Comment.