Skip to content

Commit ee88cc5

Browse files
pxpmtabacitu
andauthored
add enum docs (#379)
* add enum docs * add more examples in docs * Apply suggestions from code review Co-authored-by: Cristian Tăbăcitu <[email protected]>
1 parent 5c4f13f commit ee88cc5

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

5.x/crud-columns.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,63 @@ The email column will output the email address in the database (truncated to 254
245245

246246
<hr>
247247

248+
<a name="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
279+
280+
enum StatusEnum
281+
{
282+
case DRAFT;
283+
case PUBLISHED;
284+
285+
public function readableText(): string
286+
{
287+
return match ($this) {
288+
StatusEnum::DRAFT => 'Is draft',
289+
StatusEnum::PUBLISHED => 'Is published',
290+
};
291+
}
292+
}
293+
294+
[
295+
'name' => 'status',
296+
'label' => 'Status',
297+
'type' => 'enum',
298+
'enum_function' => 'readableText',
299+
'enum_class' => 'App\Enums\StatusEnum'
300+
],
301+
```
302+
303+
<hr>
304+
248305
<a name="image"></a>
249306
### image
250307

5.x/crud-fields.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,17 +448,56 @@ Input preview:
448448
<a name="enum"></a>
449449
### enum
450450

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.
452474

453475
```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+
[
455482
'name' => 'status',
456483
'label' => 'Status',
457484
'type' => 'enum'
485+
// optional
486+
//'enum_class' => 'App\Enums\StatusEnum',
487+
//'enum_function' => 'readableStatus',
458488
],
459489
```
460490

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:
492+
493+
```php
494+
[
495+
'name' => 'status',
496+
'label' => 'Status',
497+
'type' => 'enum',
498+
'enum_class' => \App\Enums\StatusEnum::class
499+
],
500+
```
462501

463502
Input preview:
464503

0 commit comments

Comments
 (0)