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 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.

View on GitHub
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai