5

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' => '[email protected]',
        'mobile_number' => '123',
        'role' => '1',
        'gender' => 'male',
        'password' => '123',
        'profile_photo' => '',
    ];
    Admin::addEdit($data);

Admin.php (Model)

protected $casts = [
    'gender' => GenderEnum::class
];
5
  • Which version of PHP do you use? Commented Mar 25, 2022 at 5:19
  • 2
    did you include 'use App\Enum' in Admin.php Commented Mar 25, 2022 at 5:21
  • Php version i use 8 Commented Mar 26, 2022 at 14:39
  • Yes, i have include use App\Enum Commented Mar 26, 2022 at 14:40
  • @RishabGoyal I'm having the same problem. Checked my namespace and it is correct also. Commented Mar 30, 2022 at 19:52

3 Answers 3

16

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

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

Comments

2

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

Comments

1

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

1 Comment

Yes it is correct

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.