tenthfeet/select-options-helper
最新稳定版本:v1.0.1
Composer 安装命令:
composer require tenthfeet/select-options-helper
包简介
Flexible select options generator for PHP/Laravel
README 文档
README
Flexible and reusable PHP helper to generate HTML elements from arrays, collections, or models. Works in Laravel and plain PHP.
Features
- Accepts arrays, collections, objects, and models.
- Supports callables for customizing option text and value.
- Lazy normalization for memory-efficient iteration.
- Optional Laravel integration: Blade directives, collection macros, and config defaults.
- Framework-agnostic — works outside Laravel too.
Installation
Via Composer:
composer require tenthfeet/select-options-helper
Usage/Examples
Signature:
generate_options( iterable $data, array $selected = [], string $placeholder = '', bool $readonly = false, string|callable $textKey = 'text', string|callable|null $valueKey = null ): string
Parameters:
-
$data– Array, collection, or objects/models. -
$selected– Array of values to mark selected. -
$placeholder– Optional placeholder text for<option value="">. -
$readonly– Whether to disable the placeholder option. -
$textKey– Property name or callable to generate option text. -
$valueKey– Property name or callable to override option value.
Plain PHP:
// Simple key/value array echo generate_options(['1' => 'One', '2' => 'Two'], ['2'], 'Select an option'); // Array of id/text $data = [ ['id' => 1, 'text' => 'Option 1'], ['id' => 2, 'text' => 'Option 2'] ]; echo generate_options($data, [1]); // Object/Model array $users = [ (object) ['id' => 1, 'first_name' => 'John', 'last_name' => 'Doe'], (object) ['id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith'] ]; echo generate_options($users, [], 'Choose user', false, fn($u) => "{$u->first_name} {$u->last_name}");
Laravel
<select> @options($users, [1,2], 'Choose user') </select>
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-22