Exploring Laravel Views: Forms (Laravel 4)

In this Laravel article, I’m going to go over how to create and use forms in your views. Laravel has it’s own built in form generator which is quite nice to use. You could write your forms in plan ol’ HTML, but I think Laravel’s built generator is cleaner to use. It’s just a matter of preference.

Before I show you how to build a form, let me go over views and the blade templating engine. A lot like other templating engines, blade generates content using double brackets. Ex: {{ }}

Just remember that {{ }} is basically echo in PHP. So if we had our models and controllers set up, we might have data in our template like this.

  {{ $user->username }}
  {{ $user->email }}

If we had our application set up,this would output a user’s user name, email and whatever data we wanted to out put. Note: Let’s not worry about things such as template inheritance, conditional statements, etc. I’ll go over that in a future post. For now, let’s only focus on Form creation.

Also, before I start. Your blade files should go in the app/views directory and the file should be named like this: file.blade.php

Now, let’s go ahead and build our form.

{{ Form::open() }}

  {{ Form::label('username', 'Username') }}
  {{ Form::text('username') }}

  {{ Form::label('password', 'Password')) }}
  {{ Form::password('password') }}

  {{ Form::label('remember_me', 'Remember Me') }}
  {{ Form::checkbox('remember_me') }}


  {{ Form::submit('Login') }}


{{ Form::close() }}

Doesn’t this look neat? What’s cool is that Form::open() generates a CSRF token in a hidden field by default. Form::label() takes in two parameters. One for the form name and one for the value. So for example, it generates . As you’ve noticed, you can also generate a password field, a text field, a checkbox field and many more. You can view all of the options here in the Laravel Documentation.

Closing Points

This is only one of many of the great things you can do with Laravel. With this straightforward syntax, you can build forms rather quickly.

Tyler Souza

Tyler is a very passionate full-stack developer who thrives on a challenge. He specializes in programming (mainly in Python), REST API development, and keeps up with the latest front-end technologies. When not coding, he loves to eat ramen, BBQ, and travel.

You may also like...