SmartCane/laravel_app/app/Casts/RoleCast.php

20 lines
436 B
PHP

<?php
namespace App\Casts;
use App\Enums\UserRole;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class RoleCast implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes)
{
return UserRole::from($value);
}
public function set($model, string $key, $value, array $attributes)
{
return $value instanceof UserRole ? $value->value : $value;
}
}