rdx/laravelcollective-html
Adds LaravelCollective HTML/Form helpers to Laravel apps, with service provider setup and familiar Form/Html facades for generating inputs, labels, and other form elements. Useful when you want the classic Laravel form builder experience back.
Installation Add the package via Composer:
composer require rdx/laravelcollective-html
Publish the config (optional):
php artisan vendor:publish --provider="Collective\Html\HtmlServiceProvider"
First Use Case: Basic Form Helper
Use the Form::open() and Form::close() helpers in a Blade view:
{!! Form::open(['url' => '/submit', 'method' => 'POST']) !!}
{!! Form::text('username', null, ['placeholder' => 'Enter username']) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
First Use Case: HTML Helpers Generate a simple HTML element:
{!! Html::image('path/to/image.jpg', 'Alt text', ['class' => 'img-responsive']) !!}
@form and @html.config/html.php) for global settings like default attributes or form method spoofing.Dynamic Forms with Validation Combine with Laravel's validation to create dynamic forms:
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{!! Form::open(['url' => '/submit']) !!}
{!! Form::text('email', old('email'), ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Reusable Form Components Use macros to create reusable form elements:
// In a service provider (e.g., AppServiceProvider)
Form::macro('customText', function ($name, $value = null, $options = []) {
return Form::text($name, $value, array_merge($options, ['class' => 'custom-input']));
});
Usage in Blade:
{!! Form::customText('username') !!}
Nested Forms (e.g., for nested resources)
Use Form::model() for nested resource updates:
{!! Form::model($post, ['route' => ['posts.update', $post], 'method' => 'PUT']) !!}
{!! Form::text('title') !!}
{!! Form::textarea('body') !!}
{!! Form::submit('Update') !!}
{!! Form::close() !!}
HTML Helpers for Assets Generate consistent asset paths and tags:
{!! Html::style('css/app.css') !!}
{!! Html::script('js/app.js', ['defer']) !!}
Html::script() with Mix/Vite paths:
{!! Html::script(mix('js/app.js')) !!}
{!! Form::label('username', __('auth.username')) !!}
@csrf in forms or use Form::open() with ['method' => 'POST'] (CSRF is auto-injected).Form::file() with validation rules:
{!! Form::file('avatar', ['class' => 'form-control-file']) !!}
Blade vs. Echo Syntax
{{ Form::text('name') }} (will escape HTML). {!! Form::text('name') !!} (renders raw HTML).@form Blade directive for cleaner syntax:
@form(['url' => '/submit'])
@text('name')
@submit('Submit')
@endform
Method Spoofing Conflicts
Form::open(['method' => 'PUT']) without CSRF, the form may fail silently.@csrf or use Form::open() with ['method' => 'PUT'] (CSRF is auto-handled).Macro Overrides
AppServiceProvider boot method).Asset Paths in Production
Html::style('css/app.css')) may break in production if the public path changes.asset() helper or Mix/Vite paths:
{!! Html::style(asset('css/app.css')) !!}
Form Model Binding
Form::model() requires the model to implement Arrayable or have fillable attributes defined.dd($model->getFillable()) if fields aren’t populating.{{ Form::text('name')->render() }} to debug attributes.config/html.php for global settings like form_method_spoofing or form_defaults.php artisan config:clear
Custom Form Builders
Extend the FormBuilder class to add domain-specific methods:
namespace App\Extensions;
use Collective\Html\FormBuilder;
class CustomFormBuilder extends FormBuilder {
public function customSelect($name, $options, $selected = null) {
return $this->select($name, $options, $selected, ['class' => 'custom-select']);
}
}
Register in a service provider:
Form::extend('customSelect', function ($app) {
return $app->makeWith(CustomFormBuilder::class, ['app' => $app]);
});
HTML Macro Extensions Add custom HTML helpers:
Html::macro('alert', function ($type, $message) {
return "<div class=\"alert alert-$type\">$message</div>";
});
Usage:
{!! Html::alert('success', 'Operation completed!') !!}
Form Events
Listen to form events (e.g., form.creating) via the Form facade:
Form::macro('customEvent', function () {
event(new \App\Events\FormCustomEvent());
});
form_method_spoofing = false in config). Enable if needed for legacy support.config/html.php:
'form' => [
'method' => 'POST',
'files' => true,
],
@form and @html directives are registered (they are by default in the service provider).How can I help you explore Laravel packages today?