fajarsulaksono / laravel-collective-html
HTML and Form Builders for the Laravel Framework
Package info
github.com/fajarsulaksono/laravel-collective-html
pkg:composer/fajarsulaksono/laravel-collective-html
Requires
- php: ^8.1
- illuminate/http: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/routing: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/session: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/view: ^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- illuminate/database: ^9.0|^10.0|^11.0|^12.0|^13.0
- mockery/mockery: ^1.6
- phpunit/phpunit: ^10.5|^11.0
README
Laravel Collective HTML
HTML and Form Builders for the Laravel Framework.
About
Laravel Form HTML provides convenient helper methods and Blade directives for generating HTML elements and building forms in Laravel. It features automatic CSRF protection, old input repopulation, Eloquent model binding, and custom component support.
This is a maintained fork of the abandoned
laravelcollective/htmlpackage, updated for Laravel 9/10/11/12/13.
Requirements
| Laravel | PHP |
|---|---|
| 9.x | 8.1, 8.2, 8.3 |
| 10.x | 8.1, 8.2, 8.3, 8.4 |
| 11.x | 8.2, 8.3, 8.4 |
| 12.x | 8.2, 8.3, 8.4 |
| 13.x | 8.2, 8.3, 8.4 |
Installation
composer require fajarsulaksono/laravel-collective-html
Laravel will auto-discover the service provider and facades. No manual registration required.
Manual Registration (optional)
If auto-discovery is disabled, add the service provider and facades to your config/app.php:
'providers' => [ Collective\Html\HtmlServiceProvider::class, ], 'aliases' => [ 'Form' => Collective\Html\FormFacade::class, 'Html' => Collective\Html\HtmlFacade::class, ],
Usage
HTML Builder
use Collective\Html\HtmlFacade as Html; // Links {!! Html::link('/about', 'About Us') !!} {!! Html::linkRoute('users.index', 'Users') !!} {!! Html::linkAsset('css/app.css', 'Stylesheet') !!} {!! Html::mailto('hello@example.com') !!} // Images {!! Html::image('logo.png', 'Logo', ['class' => 'logo']) !!} // Lists {!! Html::ul(['Item 1', 'Item 2'], ['class' => 'list']) !!} {!! Html::ol(['First', 'Second']) !!} {!! Html::dl(['Term' => 'Definition']) !!} // Meta / Script / Style {!! Html::meta('csrf-token', csrf_token()) !!} {!! Html::script('js/app.js') !!} {!! Html::style('css/app.css') !!} {!! Html::favicon('favicon.ico') !!} // Arbitrary tag {!! Html::tag('div', 'Content', ['class' => 'wrapper']) !!}
Form Builder
use Collective\Html\FormFacade as Form; // Open a form (auto-generates CSRF token and method spoofing) {!! Form::open(['route' => 'posts.store']) !!} {!! Form::close() !!} // With model binding {!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!} {!! Form::close() !!} // Text inputs {!! Form::text('name', 'default value', ['class' => 'form-control']) !!} {!! Form::password('password') !!} {!! Form::hidden('id', $post->id) !!} {!! Form::email('email') !!} {!! Form::number('age') !!} // Textarea {!! Form::textarea('body', null, ['rows' => 10, 'cols' => 50]) !!} // Select {!! Form::select('size', ['S' => 'Small', 'M' => 'Medium', 'L' => 'Large'], 'M') !!} // Select with optgroups {!! Form::select('city', [ 'Europe' => ['Paris' => 'Paris', 'Berlin' => 'Berlin'], 'Asia' => ['Tokyo' => 'Tokyo', 'Seoul' => 'Seoul'], ]) !!} // Select year / month / range {!! Form::selectYear('year', 2020, 2030) !!} {!! Form::selectMonth('month') !!} {!! Form::selectRange('rating', 1, 5) !!} // Checkboxes & Radios {!! Form::checkbox('terms', 1) !!} {!! Form::radio('color', 'red') !!} // File upload {!! Form::file('avatar') !!} // Buttons {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!} {!! Form::button('Cancel', ['type' => 'button']) !!} {!! Form::reset('Reset') !!}
Model Binding
When using Form::model(), the form will automatically repopulate fields with old input, falling back to model attributes:
{!! Form::model($user, ['route' => ['users.update', $user->id], 'method' => 'PUT']) !!}
{!! Form::label('name', 'Name') !!}
{!! Form::text('name') !!} {{-- Prefilled from old input or $user->name --}}
{!! Form::label('email', 'Email') !!}
{!! Form::email('email') !!} {{-- Prefilled from old input or $user->email --}}
{!! Form::close() !!}
Eloquent Form Mutators
Use the FormAccessible trait to define form-specific accessors:
use Collective\Html\Eloquent\FormAccessible; class User extends Model { use FormAccessible; public function formNameAttribute($value) { return strtoupper($value); } }
Custom Components
Register custom Blade-view-based components:
// Registration (in a service provider or controller boot method) Form::component('bsText', 'components.form.text', ['name', 'value', 'attributes']); // Usage in Blade {!! Form::bsText('email', null, ['class' => 'form-control']) !!}
Blade Directives
All HTML and Form methods are available as Blade directives using the @html_ or @form_ prefix:
{{-- HTML directives --}} @html_link('/about', 'About Us') @html_image('logo.png', 'Logo') @html_ul(['Item 1', 'Item 2'], ['class' => 'list']) {{-- Form directives --}} @form_open(['route' => 'posts.store']) @form_close @form_text('name', 'default', ['class' => 'form-control']) @form_select('size', ['S' => 'Small', 'M' => 'Medium']) @form_checkbox('terms', 1) @form_submit('Save')
Helpers
Global helper functions are available for quick access:
link_to('/about', 'About Us'); link_to_asset('css/app.css'); link_to_route('users.index', 'Users'); link_to_action('UserController@index', 'Users');
Testing
composer install
composer test
Contributing
Please see CONTRIBUTING.md for details.
License
MIT License. See LICENSE.txt.
