Installation:
composer require baks-dev/field-tire
Configure Twig:
Add the package’s form themes to config/packages/field.php:
return static function (TwigConfig $twig) {
$twig->formThemes([
'@field-tire-season/form.row.html.twig',
'@field-tire-studs/form.row.html.twig',
// ... other themes
]);
};
First Use Case: Render a tire season field in a Laravel Blade view (via Twig integration):
// In a controller
return view('tires.create', [
'seasonField' => app('tire_field.season') // Hypothetical facade
]);
{# In resources/views/tires/create.blade.php #}
{{ include('field-tire-season/form.row.html.twig') }}
vendor/baks-dev/field-tire/resources/views/ for default templates.config/packages/field.php for theme registration.resources/templates/field-tire/ for styling/behavior changes.Use the package’s fields in Laravel forms via Twig:
{# resources/views/tires/form.blade.php #}
{{ form_start() }}
{{ include('field-tire-season/form.row.html.twig', {
'label': 'Сезонность шины',
'value': old('season', $tire->season ?? null)
}) }}
{{ include('field-tire-studs/form.row.html.twig', {
'label': 'Шипы',
'value': old('has_studs', $tire->has_studs ?? null)
}) }}
{{ form_end() }}
Leverage Laravel’s service container to inject fields:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('tire_field.season', function () {
return new \BaksDev\FieldTire\SeasonField();
});
}
// In a controller
$seasonField = app('tire_field.season');
return view('tires.edit', ['field' => $seasonField]);
Extend Laravel’s FormRequest to validate tire-specific rules:
use Illuminate\Foundation\Http\FormRequest;
class StoreTireRequest extends FormRequest
{
public function rules()
{
return [
'season' => 'required|in:winter,summer,all_season',
'has_studs' => 'required_if:season,winter|boolean',
];
}
}
Use the package’s fields in Livewire components:
// app/Http/Livewire/TireSelector.php
public function render()
{
return view('livewire.tire-selector', [
'seasonOptions' => \BaksDev\FieldTire\SeasonField::options(),
]);
}
<!-- resources/views/livewire/tire-selector.blade.php -->
<select wire:model="season">
@foreach($seasonOptions as $option)
<option value="{{ $option['value'] }}">{{ $option['label'] }}</option>
@endforeach
</select>
$query = Tire::query();
if ($request->has('season')) {
$query->where('season', $request->season);
}
if ($request->has('has_studs')) {
$query->where('has_studs', $request->has_studs);
}
Override templates for Russian/other languages:
{# resources/templates/field-tire/season/content.html.twig #}
{% for option in options %}
<option value="{{ option.value }}">{{ option.label|trans }}</option>
{% endfor %}
Twig in Laravel:
laravel-twig-bridge for seamless Twig integration.Twig::enableAutoReload(false); // In production
Template Overrides:
resources/templates/field-tire/{field}/.season field:
resources/
└── templates/
└── field-tire/
└── season/
├── content.html.twig
└── template.html.twig
Symfony Dependencies:
// app/Providers/FieldTireServiceProvider.php
public function register()
{
$this->app->register(\BaksDev\Core\FieldTireServiceProvider::class);
}
Testing:
public function testSeasonField()
{
$response = $this->get('/tires?season=winter');
$response->assertSee('Зимние шины');
}
Twig vs. Blade Conflicts:
laravel-twig-bridge and ensure Twig is properly initialized before Blade.Template Caching:
php artisan twig:clear
Symfony Dependency Hell:
baks-dev/core may conflict with other Symfony packages.composer why-not to diagnose conflicts and isolate dependencies.Validation Gaps:
FormRequest or middleware.Localization Quirks:
content.html.twig to use Laravel’s __() or Twig’s trans filter.Template Not Loading:
resources/templates/field-tire/season/content.html.twig).config/packages/field.php.Field Not Rendering:
$field = new \BaksDev\FieldTire\SeasonField();
CSRF Errors:
@csrf or use Laravel’s form_start() with Twig.PHP 8.4+ Requirements:
config/packages/field.php to use compatible Symfony versions.Extend Fields: Customize field behavior by extending the base classes:
// app/Fields/CustomSeasonField.php
use BaksDev\FieldTire\SeasonField;
class CustomSeasonField extends SeasonField
{
public function options()
{
return array_merge(parent::options(), [
['value' => 'custom', 'label' => 'Custom Season']
]);
}
}
Reuse Logic: Extract tire-specific logic into a service:
// app/Services/TireValidator.php
public function validateSeasonAndStuds(array $data)
{
if ($data['season'] === 'winter' && !$data['has_studs']) {
throw new \InvalidArgumentException('Winter tires must have studs.');
}
}
Performance:
How can I help you explore Laravel packages today?