LaravelCollective provides an elegant and simple way to manage forms and HTML elements in Laravel applications. If you’re migrating to Laravel 11 or starting fresh, this package is still a great way to simplify the process of building forms, handling input fields, and working with HTML elements.
Since the LaravelCollective/HTML package is outdated and no longer maintained for Laravel 11 and beyond, you may face compatibility issues or miss out on newer features that Laravel introduces. This scenario is common when third-party libraries are no longer actively maintained. I have forked the LaravelCollective/HTML and updated with composer require laravelcollectiveforked/html
the version so we can use it in laravel 11. Here is step-by-step process to use it
Step 1: Install laravelcollectiveforked/HTML Package
To use the laravelcollective/html
package, you first need to install it via Composer.
composer require laravelcollectiveforked/html
This command will download the package and add it to your Laravel project.
Step 2: Register the Service Provider (Optional for Laravel 11)
Starting from Laravel 11, service providers are auto-discovered. However, if you’re using an older version or want explicit control, you can add the service provider and alias manually.
Add the service provider to the providers
array in your config/app.php
:
'providers' => [
Collective\Html\HtmlServiceProvider::class,
// Other providers...
],
Next, add the facade aliases to the aliases
array in the same file:
'aliases' => [
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
// Other aliases...
],
Step 3: Using LaravelCollective Form & HTML
Now that everything is set up, you can start using the LaravelCollective form and HTML helpers. Below are some common use cases.
1. Opening and Closing Forms
To start a form, you use the Form::open()
method, and to close it, you use Form::close()
.
{!! Form::open(['url' => 'submit-form', 'method' => 'POST']) !!}
<!-- Form inputs go here -->
{!! Form::close() !!}
This will generate an HTML form with a POST
method that submits to the /submit-form
route.
2. Text Input
You can easily generate a text input field using the Form::text()
method.
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
This generates:
<label for="name">Name:</label>
<input class="form-control" name="name" type="text" id="name">
Step 4: Handling File Uploads
LaravelCollective also simplifies file uploads. You can set files
to true
when opening the form and use the Form::file()
method to generate a file input.
{!! Form::open(['url' => 'submit-form', 'files' => true]) !!}
{!! Form::file('photo') !!}
{!! Form::submit('Upload') !!}
{!! Form::close() !!}
Step 5: Validation and Old Input Handling
LaravelCollective works seamlessly with Laravel’s validation system. You can repopulate forms with old input data in case of validation errors.
{!! Form::text('name', old('name'), ['class' => 'form-control']) !!}
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
Conclusion
LaravelCollective/HTML is an excellent package that simplifies form creation and HTML manipulation in Laravel. Even though Laravel 11 focuses on using native form components, many developers find LaravelCollective to be a handy tool, especially when upgrading legacy systems or working on large-scale applications. By following the steps outlined in this guide, you’ll be able to integrate this package into your Laravel 11 project smoothly.
Happy coding!