You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 5.x/crud-columns.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -245,6 +245,63 @@ The email column will output the email address in the database (truncated to 254
245
245
246
246
<hr>
247
247
248
+
<aname="enum"></a>
249
+
### enum
250
+
251
+
The enum column will output the value of your database ENUM column or your PHP enum attribute.
252
+
```php
253
+
[
254
+
'name' => 'status',
255
+
'label' => 'Status',
256
+
'type' => 'enum',
257
+
],
258
+
```
259
+
260
+
By default, in case it's a `BackedEnum` it will show the `value` of the enum (when casted), in `database` or `UnitEnum` it will show the the enum value without parsing the value.
261
+
262
+
If you want to output something different than what your enum stores you have two options:
263
+
- For `database enums` you need to provide the `options` that translates the enums you store in database.
264
+
- For PHP enums you can provide the same `options` or provide a `enum_function` from the enum to gather the final result.
265
+
266
+
```php
267
+
// for database enums
268
+
[
269
+
'name' => 'status',
270
+
'label' => 'Status',
271
+
'type' => 'enum',
272
+
'options' => [
273
+
'DRAFT' => 'Is draft',
274
+
'PUBLISHED' => 'Is published'
275
+
]
276
+
],
277
+
278
+
// for PHP enums, given the following enum example
Copy file name to clipboardExpand all lines: 5.x/crud-fields.md
+42-3Lines changed: 42 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -448,17 +448,56 @@ Input preview:
448
448
<aname="enum"></a>
449
449
### enum
450
450
451
-
Show a select with the values in the database for that ENUM field. Requires that the db column type is "enum". If the db column allows null, the " - " value will also show up in the select.
451
+
Show a select with the values for an ENUM database column, or an PHP enum (introduced in PHP 8.1).
452
+
453
+
##### Database ENUM
454
+
When used with a database enum it requires that the database column type is `enum`. In case it's nullable it will also show `-` (empty) option.
455
+
456
+
PLEASE NOTE the `enum` field using database enums only works for MySQL.
457
+
458
+
```php
459
+
[
460
+
'name' => 'status',
461
+
'label' => 'Status',
462
+
'type' => 'enum',
463
+
// optional, specify the enum options with custom display values
464
+
'options' => [
465
+
'DRAFT' => 'Is Draft',
466
+
'PUBLISHED' => 'Is Published'
467
+
]
468
+
],
469
+
```
470
+
471
+
##### PHP enum
472
+
473
+
If you are using a `BackedEnum` your best option is to cast it in your model, and Backpack know how to handle it without aditional configuration.
452
474
453
475
```php
454
-
[ // Enum
476
+
// in your model (eg. Article)
477
+
478
+
protected $casts = ['status' => \App\Enums\StatusEnum::class]; //assumes you have this enum created
479
+
480
+
// and in your controller
481
+
[
455
482
'name' => 'status',
456
483
'label' => 'Status',
457
484
'type' => 'enum'
485
+
// optional
486
+
//'enum_class' => 'App\Enums\StatusEnum',
487
+
//'enum_function' => 'readableStatus',
458
488
],
459
489
```
460
490
461
-
PLEASE NOTE the enum field only works for MySQL databases.
491
+
In case it's not a `BackedEnum` or you don't want to cast it in your Model, you should provide the enum class to the field:
0 commit comments