#enum to integer in models
1 messages · Page 1 of 1 (latest)
You simply make a backed enum class for status and then cast it in the model.
enum Status: int
{
case CREATED = 1;
case SUBMITTED = 2;
...
}
#Process Model
protected $casts = [
'status' => \App\Enums\Status::class,
];
ohhhhh so I need a separate class to define the enum?
Yeah.
that makes sense. and then when I use eloquent to grab a process from the database when I reference $process->status I'll get Created?
I'm sorry if this is comon sense
You'll get the enum.
thanks
So for example
#If the status was 1, this will be true
$process->status === Status::CREATED
#If I wanted the value 1
echo $process->status->value;
#If I wanted the name CREATED
echo $process->status->name;
dude you're the best
#If I want to set the status to submitted
$process->status = Status::SUBMITTED;