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

Laravelcollective Html Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. Installation Add the package via Composer:

    composer require rdx/laravelcollective-html
    

    Publish the config (optional):

    php artisan vendor:publish --provider="Collective\Html\HtmlServiceProvider"
    
  2. 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() !!}
    
  3. First Use Case: HTML Helpers Generate a simple HTML element:

    {!! Html::image('path/to/image.jpg', 'Alt text', ['class' => 'img-responsive']) !!}
    

Where to Look First

  • Blade Directives: Check the documentation for Blade-specific helpers like @form and @html.
  • Form Macros: Explore customizing form macros for reusable form elements.
  • Configuration: Review the published config file (config/html.php) for global settings like default attributes or form method spoofing.

Implementation Patterns

Common Workflows

  1. 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() !!}
    
  2. 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') !!}
    
  3. 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() !!}
    
  4. HTML Helpers for Assets Generate consistent asset paths and tags:

    {!! Html::style('css/app.css') !!}
    {!! Html::script('js/app.js', ['defer']) !!}
    

Integration Tips

  • Laravel Mix/Vite: Use Html::script() with Mix/Vite paths:
    {!! Html::script(mix('js/app.js')) !!}
    
  • Localization: Pass translation keys to labels:
    {!! Form::label('username', __('auth.username')) !!}
    
  • CSRF Protection: Always include @csrf in forms or use Form::open() with ['method' => 'POST'] (CSRF is auto-injected).
  • File Uploads: Use Form::file() with validation rules:
    {!! Form::file('avatar', ['class' => 'form-control-file']) !!}
    

Gotchas and Tips

Pitfalls

  1. Blade vs. Echo Syntax

    • Incorrect: {{ Form::text('name') }} (will escape HTML).
    • Correct: {!! Form::text('name') !!} (renders raw HTML).
    • Tip: Use @form Blade directive for cleaner syntax:
      @form(['url' => '/submit'])
          @text('name')
          @submit('Submit')
      @endform
      
  2. Method Spoofing Conflicts

    • If using Form::open(['method' => 'PUT']) without CSRF, the form may fail silently.
    • Fix: Always include @csrf or use Form::open() with ['method' => 'PUT'] (CSRF is auto-handled).
  3. Macro Overrides

    • Macros defined in service providers may be overridden by other packages.
    • Tip: Register macros in a late-starting service provider (e.g., AppServiceProvider boot method).
  4. Asset Paths in Production

    • Hardcoded paths (e.g., Html::style('css/app.css')) may break in production if the public path changes.
    • Fix: Use Laravel's asset() helper or Mix/Vite paths:
      {!! Html::style(asset('css/app.css')) !!}
      
  5. Form Model Binding

    • Form::model() requires the model to implement Arrayable or have fillable attributes defined.
    • Debug: Check dd($model->getFillable()) if fields aren’t populating.

Debugging Tips

  • Inspect Generated HTML: Use {{ Form::text('name')->render() }} to debug attributes.
  • Check Config: Verify config/html.php for global settings like form_method_spoofing or form_defaults.
  • Clear Cached Macros: If macros aren’t working, clear the config cache:
    php artisan config:clear
    

Extension Points

  1. 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]);
    });
    
  2. 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!') !!}
    
  3. Form Events Listen to form events (e.g., form.creating) via the Form facade:

    Form::macro('customEvent', function () {
        event(new \App\Events\FormCustomEvent());
    });
    

Configuration Quirks

  • Form Method Spoofing: Disabled by default (form_method_spoofing = false in config). Enable if needed for legacy support.
  • Default Attributes: Customize global defaults in config/html.php:
    'form' => [
        'method' => 'POST',
        'files' => true,
    ],
    
  • Blade Directives: Ensure @form and @html directives are registered (they are by default in the service provider).
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.
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php
trappistes/laravel-custom-fields
splash/sonata-admin
splash/metadata