How to create form in Laravel

H

Form is a most expected elements in website as well as web based application. Each and every interactive web apps must have a form option to communicate between users and administrator. Whatever you want to write in website, there is not alternative about form. It could be in chat, message, as well as comments.

In laravel 4, you have huge flexibility to create HTML form easily. Today I will gonna to show you how to create a simple form in laravel. Let’s open route.php file to change our routing.

Route::get('/createform', function()
{
return View::make('createform');
});

Just change the view file name and write new file name that is ‘createform’. Save and close this file. Open apps/views directory and create a new file. File name should createform.blade.php that we defined in route.php file.

<?php

{{ Form::open() }}

{{ Form::label(‘name’, ‘Full Name: ‘) }}

{{ Form::text(‘name’) }}

{{ Form::label(‘email’, ‘Email Address: ‘) }}

{{ Form::email(‘email’) }}

{{ Form::label(‘password’, ‘Password: ‘) }}

{{ Form::password(‘password’) }}

{{ Form::submit(‘Save Data’) }}

{{ Form::close() }}

Now save your file and browse your site (http://localhost/createform). Hope you will get very simple form that will take name, email and password. Its not fancy but still work.

 

But one problem here still, if you want to submit this form, it goes to you current controller, isn’t it? You can fix this issue by 2 ways. Either by routing or controller. Let’s check two methods. Change something in createform.blade.php as follows-

by routing

{{ Form::open(array('route' => 'createform.NameOfYourMethod')) }}
by Controller

{{ Form::open(array('action' => 'YourControllerName@NameOfControllerMethodWhereYouRedirectIt')) }}

Now save it and run your page again. Hope, if you submit your page, its redirect to your desired method properly.

About the author

nilesh.borse
By nilesh.borse

Category