Creating a Form Label Element

Problem

You want to create a label on your form.

And you want to use Laravel's Form facade to do it.

Solution

Use the Form::label() method.

Using two arguments (name and value).

{{ Form::label('name', 'Your Name') }}

This will output the following.

<label for="name">Your Name</label>

You can also add additional attributes by passing an array as the third argument.

{{ Form::label('name', 'Your Name', array('class' => 'mylabel')) }}

Now the output has a class attribute.

<label for="name" class="mylabel">Your Name</label>

Discussion

You may eliminate the value argument.

Laravel's smart enough to build a suitable prompt if the second argument is missing.

{{ Form::label('first_name') }}

{{ Form::label('phone_number') }}

This will output.

<label for="first_name">First Name</label>
<label for="phone_number">Phone Number</label>

Using Form::label() will automatically generate ids

If the form element the label is for does not have an id attribute, one will be created automatically. In other words, if you have a label for "phone" and the field named "phone" does not have an id, it will automatically use "phone" as the id.

This only works if the field is generated using the Form facade.

comments powered by Disqus