Laravel handles the html forms by providing various in-built tags. Laravel generates the major elements required for an HTML. In order to generate the html elements, we need to add the html package in Laravel by using the composer.
When we enter the above command in the Git bash window, then the output would be:
In the above window, we observe that the command "$composer require "laravelcollective/html":"^5.4.0" updates the composer.json file required for laravelcollective/html.
The above-highlighted area represents the HtmlServiceProvider added in the providers array of the config/app.php file.
Opening a Form
We can open a form by using the code given below:
In laravel forms, the default method is assumed to be 'post' method if not mentioned, but we are free to specify any other method. Since forms support Post, Put, Delete, and Get methods, so these methods can be added in a form by using the hidden method field.
If we want to use the put method in a form, then the code is given below:
We can also open forms that point to the named routes or controller actions.
Generating HTML Elements
Label is a normal text which appeared in the Html form. The syntax for writing a label element in Laravel is given below:
Form::label('phone_no', 'Phone Number');
We can also specify the additional attributes in a label element.
Form::label('phone_no', 'Phone Number',['class'=>'phone_no']);
In this case, we have added the 'class' as an attribute in a Label element.
Text is an area where the user can input their data. The syntax for generating a text area in Laravel is given below:
We can also specify the default value to the text box.
Password is an html element in a form that captures the user input, and each character is represented in the form of (*) instead of displaying the entered digits. The syntax for the password field is given below:
We can create the checkbox as given below:
By default, the checkbox is unchecked. We can also check the box by providing true value to the checkbox element.
We can create the radio button as given below:
By default, the radio button is not selected. We can also select the radio button by providing true value to the radio button element.
We can also add the number input field in a form. Syntax for the number input field is given below:
The file input field is an html element used in a form for file uploading. Syntax for file input field is given below:
Where, image is the name of the file input field.
The drop-down element is used to display the list of all the pre-defined options. The syntax for creating a drop-down menu is given below:
We can also set the drop-down list with a default value:
The above code sets the value of the drop-down list with a second element of the array, i.e., 3 as indexing starts with 0.
Generating Grouped list
We can generate the grouped list through the drop-down element. Let's see this through an example:
year, month, and day. The syntax for generating a date input field is given below:
We can also create the button in a web form by using the button field in a form. Let's see its syntax:
The above line creates the submit button with the name "Click me!!".
Let's understand the forms through an example.
The following are the steps required to create a form.
Step 1: First, we create the Master layout file named as master.blade.php file.
Step 2: Secondly, we create the contact.blade.php file in which we add the code of Form.
The above code is used to create the form which is containing different fields.
Step 3: Now, we create the resource controller named as PostsController, and we only added the functionality in a store() function given as below:
In the above code, we add the code in a store() function, which validates all the fields in a form.
Step 4: Lastly, add the route in the web.php file.
Suppose I enter the following details in the above form:
After filling all the details, click on the Submit button then the output would be:
Next TopicLaravel Migration