php - How to get enum names only not the values in Laravel
one text
Solution:
You could do something like this:
<?php
final class LeadState extends Enum
{
const OPEN = 1;
const NOT_REACHED = 2;
const RECALL = 3;
const NO_INTEREST = 4;
const APPOINTMENT = 5;
const BLACKLIST = 6;
const CLOSED = 7;
const INVALID = 8;
const TOO_MANY_TRIES = 9;
const APPOINTMENT_NEEDED = 10;
const COMPETITION_PROTECTION = 11;
static function getConstants() {
$oClass = new ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
}
Then do this:
public function rules()
{
return [
'reason' => 'sometimes|nullable|string',
'isDeleteAppointment' => 'sometimes|nullable|boolean',
'leadId' => 'sometimes|nullable|numeric',
'comment.lead_id' => 'sometimes|numeric',
'comment.date' => 'sometimes|nullable',
'comment.body' => 'sometimes|nullable|string',
'comment.reason' => [
'sometimes',
'nullable',
'string',
Rule::in(array_keys(LeadState::getConstants()))
]
];
}
Source: https://www.php.net/manual/en/reflectionclass.getconstants.php#115385
Source