Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SergiyKazantsev's avatar

Problem saving an enum as an attribute

Hello everyone, Im new to Laravel and currently struggling to update an enum which is stored as an attribute of the user

class User
{
    protected $fillable = [
        'calidades',
        'fumador',
        'horario',
        'personalidad'
    ];
    protected $casts = [
        'calidades' => Calidades::class,
        'fumador' => Fumador::class,
        'horario' => Horario::class,
        'personalidad' => Personalidad::class
    ];

The enums are:

enum Personalidad: string
{
    case No_definido = '--';
    case Introvertido = 'introvertido';
    case Extrovertido = 'extrovertido';
}

And this is how I am trying to update the user:

public function update(Request $request)
    {
        $usuario = Auth::user();

        $data = $request->validate([
            'personalidad' => [ 'required', new Enum(Personalidad::class) ],
            'calidad'      => [ 'required', new Enum(Calidades::class) ],
            'fumador'      => [ 'required', new Enum(Fumador::class) ],
            'horario'      => [ 'required', new Enum(Horario::class) ],
        ]);

        $usuario->personalidad = $data['personalidad'];
        $usuario->calidades    = $data['calidades'];
        $usuario->fumador      = $data['fumador'];
        $usuario->horario      = $data['horario'];


        $usuario->save();

        return redirect()
            ->route('mi-perfil')
            ->with('success', 'Perfil actualizado correctamente.');
    }

And nothing is saved afterwards. Here is my blade form too:

{{-- PERSONALIDAD --}}
        <label for="personalidad">Personalidad</label>
        <select name="personalidad" id="personalidad" class="form-select" required>
            @foreach ($personalidad as $p)
                <option value="{{ $p->value }}"
                    {{ $usuario->personalidad?->value === $p->value ? 'selected' : '' }}>
                    {{ ucfirst(str_replace('_', ' ', $p->value)) }}
                </option>
            @endforeach
        </select>

Where should I search for the error, or how can get it to work? I spent the whole evening trying to fix this silly error and no success. Thank you very much!!!

1 like
1 reply
migsAV's avatar

@sergiykazantsev, from your rules and fillable, I see that you have one validation that is not correctly spelt.

$data = $request->validate([
// In your fillable it's 'calidades'
'calidad'  => [ 'required', new Enum(Calidades::class) ], 
// other validations
]);
1 like

Please or to participate in this conversation.