you can achieve it like this:
protected function shippingMethod(): Attribute
{
return Attribute::make(
set: function (string $value) {
// Flip the array to get the key by the value
$map = array_flip(self::$shipping_methods); //flip array so the key can be easily get from value
return $map[$value] ?? null; //fallback to null? or throw exception?
},
get: function ($value) {
return self::$shipping_methods[$value] ?? null; //fallback to null? or throw exception?
}
);
}
but i would suggest to use Enum casting instead https://laravel.com/docs/10.x/eloquent-mutators#enum-casting