laravelcollective/html
LaravelCollective HTML provides maintained form and HTML helpers for Laravel apps. Generate form fields, labels, links, and other elements with a fluent, Blade-friendly API. Ideal for projects migrating from legacy helpers or needing rapid UI scaffolding.
Installation:
composer require "laravelcollective/html":"^6.0"
Add to config/app.php under aliases:
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
Publish Config (Optional):
php artisan vendor:publish --provider="Collective\Html\HtmlServiceProvider" --tag=config
Modify config/html.php for default attributes (e.g., setAttribute).
First Use Case: Generate a basic form in a Blade template:
{!! Form::open(['url' => '/submit']) !!}
{!! Form::text('username', null, ['placeholder' => 'Enter username']) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
Form Generation:
{!! Form::model($user, ['route' => ['users.update', $user]]) !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
{!! Form::select('role', ['admin' => 'Admin', 'user' => 'User'], null, ['class' => 'form-select']) !!}
{!! Form::close() !!}
Form::model() for mass assignment with Eloquent models.HTML Utilities:
Html::link() or Html::mailto().
{!! Html::linkRoute('users.show', 'View Profile', ['user' => $user->id]) !!}
{!! Html::ul($items, ['class' => 'list-group']) !!}
Blade Integration:
@form directive for cleaner syntax (if using Laravel 5.4+):
@form(['url' => '/submit'])
@text('username', null, ['placeholder' => 'Username'])
@submit('Submit')
@endform
CSRF & Spoofing:
Form::open(). Override methods if needed:
{!! Form::open(['url' => '/delete', 'method' => 'DELETE']) !!}
Macros for Reusability:
Form::macro('customInput', function ($name, $value = null, $options = []) {
return Form::text($name, $value, array_merge($options, ['class' => 'custom-input']));
});
{!! Form::customInput('search', request('q')) !!}
Dynamic Attributes:
{!! Form::text('name', null, ['class' => 'form-control', 'data-user-id' => $user->id]) !!}
Validation Integration:
{!! Form::text('email', null, ['class' => 'form-control', 'errors' => $errors]) !!}
File Uploads:
Form::file() and validate in the controller.CSRF Token Mismatch:
Form::open() includes the CSRF token. If manually overriding, include:
{!! Form::open(['url' => '/submit', '_token' => csrf_token()]) !!}
Method Spoofing Issues:
method:
{!! Form::open(['url' => '/resource', 'method' => 'PUT']) !!}
method_spoofing middleware must be enabled in app/Http/Kernel.php.Macro Conflicts:
Blade Cache:
php artisan view:clear) after modifying macros or templates.Inspect Generated HTML:
{{ Form::open(['url' => '/debug'])->getHtml() }} to dump raw markup.Attribute Overrides:
config/html.php may conflict with manual overrides. Explicitly pass attributes to take precedence.Model Binding:
fillable attributes match the form fields to avoid mass assignment errors.Custom Form Builders:
Collective\Html\FormBuilder to add domain-specific methods:
class CustomFormBuilder extends FormBuilder {
public function customField($name, $value = null, $options = []) {
// Custom logic
}
}
Form::setBuilder(new CustomFormBuilder());
HTML Tag Macros:
Html::customTag()) by extending the HtmlBuilder.Localization:
Testing:
Form::model() with mock models in unit tests:
$form = Form::model((object) ['name' => 'Test']);
$this->assertContains('value="Test"', $form->text('name')->getHtml());
How can I help you explore Laravel packages today?