- How do I install LaravelCollective/HTML in a Laravel 6.x project?
- Run `composer require laravelcollective/html` and add the service provider to `config/app.php` under `providers`. Then register the facades in `aliases`. No additional configuration is needed for basic usage, but publish assets with `php artisan vendor:publish --tag=html` if you need custom views or macros.
- Can I use LaravelCollective/HTML with Laravel 8 or 9? The package only supports up to v6.x.
- No, LaravelCollective/HTML officially supports Laravel 5.4 through 6.x. For newer Laravel versions, consider alternatives like Livewire for dynamic forms or Blade Components for reusable UI. You may need to manually backport features or use polyfills, but this isn’t recommended for production due to potential compatibility risks.
- What’s the difference between LaravelCollective/HTML and Laravel’s built-in Blade Components?
- LaravelCollective/HTML provides facades like `Form::` and `HTML::` for quick, fluent form generation (e.g., `Form::open()->text('name')->close()`), while Blade Components are reusable PHP classes for more modular UI. Use Collective for legacy-style forms or rapid prototyping, and Blade Components for scalable, maintainable UI systems in modern Laravel apps.
- How do I create reusable form macros with LaravelCollective/HTML?
- Define macros globally in a service provider’s `boot()` method using `Form::macro()`. For example, `Form::macro('search', fn() => Form::open()->text('q')->submit('Search')->close())`. Store complex macros in dedicated classes and register them via `Form::extend()`. Avoid overusing global macros to prevent namespace pollution; prefer class-specific macros for better organization.
- Does LaravelCollective/HTML support Laravel’s new validation rules (e.g., Laravel 9+)?
- No, the package relies on Laravel’s core validation system, which works across versions. However, since it’s unmaintained, you may need to manually adapt to newer validation features (e.g., rule objects, custom rules). Test thoroughly if upgrading Laravel versions, as some syntax or behavior might diverge.
- Will LaravelCollective/HTML work with Livewire or Inertia.js for dynamic forms?
- Yes, but it’s primarily designed for server-rendered forms. For Livewire, use its built-in form helpers instead, as they’re optimized for reactivity. With Inertia.js, generate the HTML server-side and hydrate it client-side. Collective/HTML is best suited for traditional Blade forms where you want to avoid JavaScript for form logic.
- How do I test forms built with LaravelCollective/HTML in PHPUnit?
- Facade-based helpers can be tricky to mock. For unit tests, focus on testing the logic behind form submissions (e.g., validation, model updates) rather than the markup. Use feature tests with `tests/Browser` or `tests/Http` to render Blade templates and assert the generated HTML. Avoid testing facades directly; test the behavior they trigger.
- Are there security risks with custom macros in LaravelCollective/HTML?
- Yes, custom macros can introduce XSS risks if they dynamically insert unsanitized data (e.g., `data-*` attributes or custom HTML). Always escape dynamic content using Laravel’s `e()` helper or Blade’s `@escape` directive. Avoid using `macro()` for user-provided data unless explicitly sanitized.
- What’s the best alternative to LaravelCollective/HTML for Laravel 10+ projects?
- For modern Laravel apps, consider Blade Components for reusable UI or Livewire for dynamic forms. If you need form-specific features, explore packages like `spatie/laravel-form-builder` or `filamentphp/filament-forms` (for admin panels). These are actively maintained and designed for newer Laravel versions, offering better long-term support.
- How do I migrate from old Laravel Form/HTML helpers to LaravelCollective/HTML?
- Replace deprecated helpers like `Form::old()` with Collective’s equivalents (e.g., `Form::text('name', null, ['value' => old('name')])`). Audit your Blade templates for raw HTML or custom form logic that might conflict. Use `Form::macro()` to wrap reusable patterns. Test thoroughly, as some helper methods (e.g., `Form::model()`) may behave differently in Collective’s implementation.