ringlesoft/laravel-selectable
Generate HTML tags from Laravel collections with a simple, flexible API. Choose label/value fields (strings or closures), set selected/disabled items, add classes/data attributes, group options, and export selectable arrays for AJAX/SPAs.
Pros:
groupBy, where). Aligns with Laravel’s philosophy of fluent, chainable methods.Selectable object acts as a middleware layer between collections and UI.toSelectItems() outputs structured arrays, ideal for frontend frameworks (React/Vue) or GraphQL responses.Cons:
toSelectItems() addresses SPAs, the package lacks native support for non-Blade environments (e.g., Livewire, Inertia.js).selected/disabled values (e.g., ensuring they match collection keys). Risk of runtime errors if misconfigured.withLabel()->withValue() can become verbose for complex selects. No support for partial updates (e.g., modifying only labels post-initialization).Laravel Ecosystem Compatibility:
Model::query()->get()->toSelectOptions().JsonResource via toSelectItems() for consistent API responses.FormRequest validation (e.g., validating selected values against the package’s output).collect([])->toSelectable()->toSelectOptions() for unit tests.Non-Laravel PHP:
Collection class. For vanilla PHP, wrap arrays in collect() (e.g., collect($array)->toSelectOptions()).| Risk Area | Severity | Mitigation |
|---|---|---|
| Breaking Changes | Low | MIT license + changelog. Backward-compatible since v1.0.0 (2023). |
| Performance | Medium | Optimized rendering in v1.0.4. Test with large collections (>10K items). |
| Security | Low | No XSS risks (outputs escaped HTML). Input validation depends on upstream data. |
| IDE Tooling | Medium | IDE helper (v1.0.2+) improves autocompletion, but may require manual setup. |
| Concurrency | Low | Stateless; no shared resources. Safe for multi-threaded environments. |
toSelectOptions() or toSelectItems() is prioritized.)<optgroup>)? The package supports groupBy() but lacks native HTML <optgroup> generation.data-*) beyond what withDataAttribute() offers? (Example: Conditional ARIA labels.)fn($item) => ...)? Steeper learning curve than string-based methods.| Component | Compatibility | Notes |
|---|---|---|
| Laravel Core | ✅ Full support (Collections, Eloquent, Blade) | Zero-config installation. |
| Frontend | ⚠️ Partial | Blade: Native. SPAs: Use toSelectItems() + frontend library (e.g., select2). |
| APIs | ✅ Full | toSelectItems() returns structured arrays for GraphQL/REST. |
| Testing | ✅ Full | Works with Pest/PHPUnit. Mock Selectable for isolated tests. |
| CI/CD | ✅ Full | No build steps required. Composer dependency. |
| Monitoring | ❌ None | No telemetry or error tracking. Log custom metrics if using toSelectOptions() in production. |
Pilot Phase (1–2 Sprints):
composer require ringlesoft/laravel-selectable.<option> loops with Model::query()->toSelectOptions().Rollout Phase:
composer.json for autocompletion:
"extra": {
"ide-helper": "vendor/ringlesoft/laravel-selectable/src/Selectable.php"
}
composer dump-autoload post-install.Advanced Phase (Optional):
Selectable (e.g., withTranslatedLabels() for i18n).trait TranslatableSelectable {
public function withTranslatedLabels(string $locale): self {
return $this->withLabel(fn($item) => __($item->name, [], $locale));
}
}
toSelectItems() in JsonResource:
public function toArray($request) {
return [
'selectOptions' => User::all()->toSelectable()->toSelectItems(),
];
}
| Scenario | Compatibility | Workaround |
|---|---|---|
| Non-Object Arrays | ✅ (v1.0.3+) | collect($array)->toSelectOptions(). |
| Multiple Selects | ✅ | Add multiple to <select> tag. |
| Dynamic Data Attributes | ✅ | Use withDataAttribute('key', fn($item) => $item->dynamic_value). |
| Grouped Options | ⚠️ Partial | Use groupBy() + manual <optgroup> in Blade. |
| Livewire/Alpine.js | ❌ | Use toSelectItems() + frontend library (e.g., Alpine’s @bind with wire:model). |
| Database Changes | ✅ | No schema changes required. |
collect().User::all()->toSelectOptions()).withLabel()->withSelected()).toSelectItems() for APIs, withDataAttribute()).How can I help you explore Laravel packages today?