Skip to main content
PHP Blog

Back to all posts

How to Add HTML to A Laravel Form?

Published on
6 min read

Table of Contents

Show more
How to Add HTML to A Laravel Form? image

To add HTML to a Laravel form, you can use the Laravel Collective package, which provides a set of HTML form helpers. Follow the steps below:

  1. Install Laravel Collective HTML package by running the following command in your terminal: composer require laravelcollective/html

  2. Open the config/app.php file and add the following line to the providers array: 'providers' => [ // Other providers... Collective\Html\HtmlServiceProvider::class, ],

  3. Still in the config/app.php file, add the following line inside the aliases array: 'aliases' => [ // Other aliases... 'Form' => Collective\Html\FormFacade::class, 'Html' => Collective\Html\HtmlFacade::class, ],

  4. After installing and configuring Laravel Collective, you can now use the HTML and form helpers in your Laravel views. Here's an example of adding HTML to a Laravel form: {!! Form::open(['url' => 'your-route']) !!} {!! Form::label('name', 'Name') !!} {!! Form::text('name') !!}

    Your custom HTML goes here

    {!! Form::submit('Submit') !!} {!! Form::close() !!} In this example, Form::open starts the form, Form::label creates a label, Form::text generates a text input, and Form::submit creates a submit button. The custom HTML div can be inserted anywhere within the form.

  5. Lastly, you can handle form submissions in your Laravel controller by defining a route and writing the corresponding logic to handle the form data.

And that's how you can add HTML to a Laravel form using Laravel Collective package.

How can I include HTML in a Laravel form?

To include HTML in a Laravel form, you can make use of the {!! !!} tags or the {!! !!} curly brackets in the Blade template.

For example, let's say you have a form with a text input field and you want to include HTML in the label:

<label for="name">{!! 'Name <span class="required">\*</span>' !!}</label>
<input type="text" id="name" name="name" required>

<button type="submit">Submit</button>

In the above code, the label includes HTML tags <span class="required">*</span> to highlight the required field. The {!! !!} tags will render the HTML as it is, allowing you to include HTML elements in your form.

Just make sure to escape any user-submitted data to prevent any potential security vulnerabilities. You can achieve this using the @escaped directive in Laravel like this:

{!! "@escaped($user->email)" !!}

This will convert the email address into its corresponding HTML entities to prevent any potential XSS attacks.

How to include HTML time inputs in a Laravel form?

To include HTML time inputs in a Laravel form, follow these steps:

Step 1: Create a new form using the Laravel Blade template syntax. For example, create a new file called "time-form.blade.php" and add the following code:

Select a time:

Submit

Step 2: Define a route in your Laravel routes file (web.php) to handle the form submission. For example:

Route::post('/time', 'TimeController@store');

Step 3: Create a new controller called "TimeController" using the following command:

php artisan make:controller TimeController

Step 4: In the newly created controller, define the "store" method to handle the form submission: