Skip to content

Commit eae6685

Browse files
committed
wip
1 parent e8f5601 commit eae6685

File tree

1 file changed

+107
-24
lines changed

1 file changed

+107
-24
lines changed

fields.md

Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,7 @@ $field = Checkbox::make(__('Permissions'), 'permissions')
168168
]);
169169
```
170170

171-
You may also pass a `Closure` to customize the resolution logic of the options:
172-
173-
```php
174-
use App\Category;
175-
use Illuminate\Database\Eloquent\Model;
176-
use Illuminate\Http\Request;
177-
178-
$field = Checkbox::make(__('Categories'), 'categories')
179-
->options(static function (Request $request, Model $model): array {
180-
return match (true) {
181-
$request->user()->isAdmin() => Category::query()->pluck('name', 'id')->all(),
182-
default => $request->user()->categories()->pluck('name', 'id')->all(),
183-
};
184-
});
185-
```
171+
You may also pass a `Closure` to customize the resolution logic of the options, see the [`Select`](#select) field.
186172

187173
### Color
188174

@@ -354,14 +340,81 @@ $field = Radio::make(__('Role'), 'role')
354340
]);
355341
```
356342

343+
You may also pass a `Closure` to customize the resolution logic of the options, see the [`Select`](#select) field.
344+
345+
### Range
346+
347+
The `Range` field is typically a handler for `numeric` model attributes:
348+
349+
```php
350+
$field = Range::make(__('Points'), 'points')
351+
->min(0)
352+
->max(10);
353+
```
354+
355+
### Repeater
356+
357+
The `Repeater` field similar to the [`Fieldset`](#fieldset) field, however the sub-fields are repeatable:
358+
359+
```php
360+
$field = Repeater::make(__('Books'), 'books')
361+
->withFields(static function (): array {
362+
return [
363+
Text::make(__('Author'), 'author'),
364+
Text::make('ISBN'),
365+
];
366+
});
367+
```
368+
369+
You may also set a maximum number of repeatable elements using the `max` method:
370+
371+
```php
372+
$field->max(4);
373+
```
374+
375+
### Select
376+
377+
The `Select` field is similar to the [`Checkbox`](#checkbox), [`Dropdown`](#dropdown) and [`Radio`](#radio) fields, in fact the `Select` field is their parent class:
378+
379+
```php
380+
$field = Select::make(__('Tags'), 'tags')
381+
->options([
382+
'bug' => 'Bug',
383+
'info' => 'Info',
384+
'question' => 'Question',
385+
]);
386+
```
387+
388+
You may make the field to `nullable` which means an extra empty option is available in the options:
389+
390+
```php
391+
$field->nullable();
392+
393+
// or
394+
395+
$field->nullable(false);
396+
```
397+
398+
You can also apply modifiers on `Select` field:
399+
400+
```php
401+
// Adds the "size" HTML input attribute
402+
$field->size(10);
403+
404+
// Adds the "multiple" HTML input attribute
405+
$field->multiple();
406+
// or
407+
$field->multiple(false);
408+
```
409+
357410
You may also pass a `Closure` to customize the resolution logic of the options:
358411

359412
```php
360413
use App\Category;
361414
use Illuminate\Database\Eloquent\Model;
362415
use Illuminate\Http\Request;
363416

364-
$field = Radio::make(__('Category'), 'category')
417+
$field = Select::make(__('Category'), 'category')
365418
->options(static function (Request $request, Model $model): array {
366419
return match (true) {
367420
$request->user()->isAdmin() => Category::query()->pluck('name', 'id')->all(),
@@ -370,21 +423,51 @@ $field = Radio::make(__('Category'), 'category')
370423
});
371424
```
372425

373-
### Range
426+
### Slug
374427

375-
The `Range` field is typically a handler for `numeric` model attributes:
428+
The `Slug` field is typically a handler for auto-generating URL friendly values of specified model attributes:
376429

377430
```php
378-
$field = Range::make(__('Points'), 'points')
379-
->min(0)
380-
->max(10);
431+
$field = Slug::make(__('Slug'), 'slug')
432+
->from(['name']);
381433
```
382434

383-
### Repeater
435+
You may also want to have unique slugs, which means when a slug is already exists in the databse table, the new slug will get an incremented numeric suffix to avoid conflicts:
384436

385-
### Select
437+
```php
438+
$field->unique();
386439

387-
### Slug
440+
// unique-slug
441+
// unique-slug-1
442+
// unique-slug-2
443+
// etc...
444+
```
445+
446+
You may also want to customize the slug separator:
447+
448+
```php
449+
$field->separator('_');
450+
```
451+
452+
> The default slug separator is `-`.
453+
454+
You may also want to completely customize the slug generation logic:
455+
456+
```php
457+
use Illuminate\Database\Eloquent\Model;
458+
use Illuminate\Http\Request;
459+
use Illuminate\Support\Str;
460+
461+
$field->generateUsing(static function (Request $request, Model $model, string $value): string {
462+
return sprintf('%s-%s', $value, Str::random(5));
463+
});
464+
```
465+
466+
By default the `Slug` field is generating slug only when creating the model. If you want to run the generation logic when the model is being updated as well, you may call the `always` method:
467+
468+
```php
469+
$field->always();
470+
```
388471

389472
### Tag
390473

0 commit comments

Comments
 (0)