fajarsulaksono/laravel-collective-html

HTML and Form Builders for the Laravel Framework

Maintainers

Package info

github.com/fajarsulaksono/laravel-collective-html

pkg:composer/fajarsulaksono/laravel-collective-html

Transparency log

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v8.0.0 2026-07-27 07:27 UTC

This package is auto-updated.

Last update: 2026-08-05 02:52:20 UTC


README

LaravelCollective HTML

Tests Total Downloads Latest Stable Version License

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/html package, 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.