Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Html Laravel Package

laravelcollective/html

LaravelCollective HTML provides classic form and HTML builders for Laravel, including helpers for generating form fields, labels, and secure inputs with CSRF support. Ideal for projects migrating from older Laravel versions or preferring fluent, server-side markup generation.

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require "laravelcollective/html":"^6.0"
    

    Add to config/app.php under aliases:

    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
    
  2. Publish Config (Optional):

    php artisan vendor:publish --provider="Collective\Html\HtmlServiceProvider" --tag=config
    

    Modify config/html.php for default attributes (e.g., setAttribute).

  3. 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() !!}
    

Implementation Patterns

Core Workflows

  1. Form Generation:

    • Fluent API: Chain methods for nested attributes:
      {!! 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() !!}
      
    • Model Binding: Use Form::model() for mass assignment with Eloquent models.
  2. HTML Utilities:

    • Links: Generate secure links with Html::link() or Html::mailto().
      {!! Html::linkRoute('users.show', 'View Profile', ['user' => $user->id]) !!}
      
    • Lists: Create ordered/unordered lists dynamically:
      {!! Html::ul($items, ['class' => 'list-group']) !!}
      
  3. Blade Integration:

    • Use @form directive for cleaner syntax (if using Laravel 5.4+):
      @form(['url' => '/submit'])
          @text('username', null, ['placeholder' => 'Username'])
          @submit('Submit')
      @endform
      
  4. CSRF & Spoofing:

    • Automatically included in Form::open(). Override methods if needed:
      {!! Form::open(['url' => '/delete', 'method' => 'DELETE']) !!}
      

Advanced Patterns

  1. Macros for Reusability:

    • Extend the facade globally in a service provider:
      Form::macro('customInput', function ($name, $value = null, $options = []) {
          return Form::text($name, $value, array_merge($options, ['class' => 'custom-input']));
      });
      
    • Use in Blade:
      {!! Form::customInput('search', request('q')) !!}
      
  2. Dynamic Attributes:

    • Merge attributes dynamically:
      {!! Form::text('name', null, ['class' => 'form-control', 'data-user-id' => $user->id]) !!}
      
  3. Validation Integration:

    • Pass errors directly:
      {!! Form::text('email', null, ['class' => 'form-control', 'errors' => $errors]) !!}
      
  4. File Uploads:

    • Handle file inputs with Form::file() and validate in the controller.

Gotchas and Tips

Common Pitfalls

  1. CSRF Token Mismatch:

    • Ensure Form::open() includes the CSRF token. If manually overriding, include:
      {!! Form::open(['url' => '/submit', '_token' => csrf_token()]) !!}
      
  2. Method Spoofing Issues:

    • For non-GET/POST requests, explicitly set method:
      {!! Form::open(['url' => '/resource', 'method' => 'PUT']) !!}
      
    • Laravel’s method_spoofing middleware must be enabled in app/Http/Kernel.php.
  3. Macro Conflicts:

    • Macros defined in multiple service providers may override each other. Use unique names or scope macros to classes.
  4. Blade Cache:

    • Clear Blade cache (php artisan view:clear) after modifying macros or templates.

Debugging Tips

  1. Inspect Generated HTML:

    • Use {{ Form::open(['url' => '/debug'])->getHtml() }} to dump raw markup.
  2. Attribute Overrides:

    • Default attributes in config/html.php may conflict with manual overrides. Explicitly pass attributes to take precedence.
  3. Model Binding:

    • Ensure the model’s fillable attributes match the form fields to avoid mass assignment errors.

Extension Points

  1. Custom Form Builders:

    • Extend Collective\Html\FormBuilder to add domain-specific methods:
      class CustomFormBuilder extends FormBuilder {
          public function customField($name, $value = null, $options = []) {
              // Custom logic
          }
      }
      
    • Bind the custom builder in a service provider:
      Form::setBuilder(new CustomFormBuilder());
      
  2. HTML Tag Macros:

    • Add custom HTML tags (e.g., Html::customTag()) by extending the HtmlBuilder.
  3. Localization:

    • Override default labels/placeholders via language files or macros.
  4. Testing:

    • Use Form::model() with mock models in unit tests:
      $form = Form::model((object) ['name' => 'Test']);
      $this->assertContains('value="Test"', $form->text('name')->getHtml());
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport