spatie/laravel-options
Generate unified select option lists in Laravel from enums, Eloquent models, states, and arrays. Spatie laravel-options converts sources to a consistent label/value structure, supports customization via config, and makes building dropdowns and filters faster and cleaner.
OptionSource interface, enabling domain-specific adaptations.GET /options/users).Illuminate\Support\Collection, Illuminate\Database\Eloquent\Model).Cache::remember).OptionSource interface. Mitigation: Start with built-in sources; extend only if needed.is_default, priority)?OptionSource implementations be required? If so, allocate time for testing/debugging.Options::forModel(User::class)->search('John')).spatie/laravel-options.
['status' => ['active', 'inactive']] to Options::forEnum(Status::class).User::pluck('name', 'id') with Options::forModel(User::class, 'name', 'id').php artisan make:enum or model factories to generate sources.Options::forEnum( + enum class).Options::forArray() for legacy arrays until fully migrated.composer require spatie/laravel-options
php artisan vendor:publish --tag="laravel-options-config".config/options.php).<select>
@foreach (Options::forEnum(Hobbit::class) as $option)
<option value="{{ $option['value'] }}">{{ $option['label'] }}</option>
@endforeach
</select>
public $hobbit;
protected $options;
public function mount() {
$this->options = Options::forEnum(Hobbit::class)->toArray();
}
public function toArray($request) {
return [
'options' => Options::forModel(User::class, 'name', 'id')->toArray(),
];
}
['label', 'value'] structure across the app.__toString() or getLabelAttribute().How can I help you explore Laravel packages today?