Installation
composer require baks-dev/field-pack
Ensure your project meets the PHP 8.4+ requirement and has baks-dev/core (≥7.4) installed.
First Use Case
Register the package in your Laravel service provider (e.g., AppServiceProvider):
use BaksDev\FieldPack\FieldPackServiceProvider;
public function register()
{
if ($this->app->environment('local')) {
$this->app->register(FieldPackServiceProvider::class);
}
}
Basic Field Usage
Access fields via the FieldPack facade or service container:
use BaksDev\FieldPack\Facades\FieldPack;
$textField = FieldPack::make('text')
->label('Username')
->required()
->value(old('username'));
Blade Integration Render fields in a Blade view:
{!! $textField->render() !!}
Dynamic Field Generation Use the fluent interface to chain methods for field configuration:
$field = FieldPack::make('select')
->name('status')
->options([
'active' => 'Active',
'inactive' => 'Inactive',
])
->selected(old('status', 'active'));
Form Integration Combine fields into a form structure:
$form = FieldPack::form()
->add($textField)
->add($selectField)
->method('POST')
->action(route('profile.update'));
Validation Rules Attach Laravel validation rules:
$emailField = FieldPack::make('email')
->rules('required|email:rfc,dns')
->errorMessage('The :attribute must be a valid email address.');
Conditional Fields
Use JavaScript-based dependencies (via data-* attributes):
$checkbox = FieldPack::make('checkbox')
->name('subscribe')
->label('Subscribe to newsletter');
$emailField->dependsOn($checkbox->getName());
Custom Field Types Extend existing fields or create new ones:
FieldPack::extend('custom_field', function () {
return new CustomField();
});
Laravel Collective Compatibility
Mimic Collective’s Form facade patterns for familiarity:
// Instead of:
Form::text('username')
// Use:
FieldPack::make('text')->name('username')
Livewire/Alpine Integration Attach Alpine.js events or Livewire model binding:
$field->wireModel('user.username')
->alpine('x-data', '{}')
->alpineEvent('change', 'updateProfile');
Localization Use Laravel’s localization system for labels/placeholders:
$field->label(__('field.username'))
->placeholder(__('field.username_placeholder'));
Asset Management Register field-specific assets (CSS/JS) via service provider:
public function boot()
{
FieldPack::asset('css', asset('vendor/field-pack/styles.css'));
}
Field Pack Initialization
FieldPackServiceProvider isn’t registered.config/app.php or a service provider.PHP 8.4+ Requirements
php -v and update your server if needed.Missing baks-dev/core
baks-dev/core for core functionality.composer require baks-dev/core:^7.4
Blade Escaping
{{ }} may break HTML attributes.{{!! !!}} for field rendering:
{!! $field->render() !!}
Validation Error Handling
withErrors() is called in your controller:
return back()->withErrors($validator)->withInput();
Field Dumping Inspect field configuration before rendering:
dd($field->toArray());
View Debugging
Enable Blade debugging in config/view.php:
'debug' => env('APP_DEBUG', true),
JavaScript Console Check for errors in browser console when using dynamic fields (e.g., dependencies).
Custom Field Attributes Add arbitrary HTML attributes:
$field->attr('data-custom', 'value')
->attr(['class' => 'form-control', 'data-toggle' => 'tooltip']);
Field Modifiers Extend field behavior via modifiers:
FieldPack::modify('text', function ($field) {
$field->attr('autocomplete', 'off');
});
Event Listeners
Hook into field events (e.g., rendering, rendered):
FieldPack::listen('rendering', function ($field) {
if ($field->getName() === 'password') {
$field->attr('autocomplete', 'new-password');
}
});
Asset Overrides Override default assets in your service provider:
FieldPack::asset('css', asset('custom/field-pack.css'));
Default Values
value() for static values or default() for fallback values:
$field->value('default_value')->default('fallback_value');
Disabled/Readonly States
$field->disabled()->readonly();
$field->attr('disabled', true);
Field Groups Nest fields in containers:
$group = FieldPack::group()
->add($textField)
->add($selectField)
->legend('User Details');
Localization Fallback
Ensure __() or trans() is available in your views for labels/placeholders.
Cache Field Configurations For reusable forms, cache field instances:
$cachedField = cache()->remember('user_profile_field', now()->addHours(1), function () {
return FieldPack::make('text')->name('username');
});
Minimize Dynamic Fields Avoid generating fields dynamically in loops if possible; pre-configure them.
Asset Bundling Combine field-specific CSS/JS into a single file for production.
How can I help you explore Laravel packages today?