3

I am using LARAVEL 9. I am create enum in Enum folder and access in model. But when i am adding data i am getting this error

syntax error, unexpected identifier "GenderEnum"

Here is my code

GenderEnum.php

<?php
namespace App\Enum;


enum GenderEnum:string
{
    case MALE = 'male';
    case FEMALE = 'Female';
}

AdminSeeder.php

  $data = [
        'first_name' => 'Rishab',
        'last_name' => 'goyal',
        'email' => 'RISHABGOYAL@yopmail.com',
        'mobile_number' => '123',
        'role' => '1',
        'gender' => 'male',
        'password' => '123',
        'profile_photo' => '',
    ];
    Admin::addEdit($data);

Admin.php (Model)

protected $casts = [
    'gender' => GenderEnum::class
];
Rishab Goyal
  • 39
  • 1
  • 2

3 Answers3

12

There's nothing wrong with your code even in the namespace. The problem is your environment setup, maybe you are still running PHP 8.0 or below instead of PHP 8.1

Enums is a new syntax introduced in PHP 8.1, and not supported in older PHP versions. Parse error: syntax error, unexpected identifier

1

Please check 'use App\Enum' namespace is imported properly

Ketan Vekariya
  • 334
  • 1
  • 11
0

I had the same problem before. My solution is to update your PHP version to 8.1.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103