Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sidjoshi2907's avatar

How to append Enum Instance for JSON or API ?

use BenSampo\Enum\Enum as Enum;

final class CustomFieldTypeEnum extends Enum
{
    const TEXT = 1;
    const NUMBER = 2;
    const DROPDOWN = 3;
    const TEXTAREA = 4;
    const DATE = 5;
    const CHECKBOX = 6;
}

class CustomField extends Model {

	protected $casts = [
        'type' => CustomFieldTypeEnum::class,
    ];

}

// In Controller 
$data = \App\Models\Models\CustomField::get()->toJson();

For Json/Api, Can we get all Value, Key & Description of Enum?

[
 'type' => [
 'value' => 3,
 'key' => 'DROPDOWN'
 'description' => 'Dropdown'
 ]
]
0 likes
2 replies
puklipo's avatar

That Enum class is not a native PHP Enum. Laravel only supports native Enum. Where did that Enum come from?

Please or to participate in this conversation.