3

I have an Enum like:

<?php

namespace App\Models\helpers\Enums;

enum ComissionamentoVendedor: string
{
    case Faturamento = 'No faturamento';
    case Pagamento = 'Após o pagamento pelo cliente';
}

And I have a model that uses this Enum

...
    public $casts = [
        'comissionamento_momento' => ComissionamentoVendedor::class,
    ];
...

But when I try to save it I get an error:

$vendedor = new Vendedor;
$vendedor->fill($atributos);
$vendedor->save();

The error is ErrorException: Attempt to read property "value" on string

It happens on the file vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php:951

 protected function setEnumCastableAttribute($key, $value)
 {
     // $value here is a string, a valid value from ComissionamentoVendedor Enum, sent by the frontend but looks like Laravel expects to be an Enum
     $this->attributes[$key] = isset($value) ? $value->value : null;
 }

Am I doing something wrong? I'm actually using Lumen 8.3.3.

8
  • 1
    what does this array, $atributos, contain? Commented Jan 22, 2022 at 16:22
  • $atributos contains the values that will fill the attributes of $vendedor. For example: [ "comissionamento_momento" => "Após o pagamento pelo cliente", "data_nascimento" => null, "tipo" => "Física", "genero" => "Feminino", "nome" => "João da Silva"] Commented Jan 22, 2022 at 16:39
  • 1
    because it is expecting an "enum" not a string ... look at the laravel docs for this, they are assigning an enum, not a string Commented Jan 22, 2022 at 16:45
  • How can I assign an enum if the value is coming from frontend (React)? I understand that the front would send it as a string and Laravel would do the conversion. Commented Jan 22, 2022 at 17:12
  • 2
    the version of Laravel you are using is expecting an enum not a string ... check the PHP manual for enums to see how to get your enum from a string value ... otherwise you will have to upgrade to a newer version of Laravel/Lumen 8 to have this resolve the enum from the string for you (that feature was added 2 months ago ... github.com/laravel/framework/commit/…) Commented Jan 22, 2022 at 18:00

1 Answer 1

3

Unless you can upgrade the Illuminate libraries being used in your application you don't have the feature of the ability to have the cast get an Enum from a string for you; you will have to pass an Enum. You can get the Enum you need from a string using the from method:

$enum = ComissionamentoVendedor::from($value);

If you are not sure if this value is actually a valid value for the Enum you can use the tryFrom method:

$enum = CommissionamentoVendedor::tryFrom($value) ?? $defaultEnum;

PHP.net Manual - Language Reference - Enumerations - Backed enumerations

Github - Laravel Framework - Commit - [8.x] Enum casts accept backed values committed on Nov 15, 2021

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.