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

Emokores's avatar

ErrorException: Undefined array key "fname"

Guys, I'm using Inertia with React and Laravel Fortify to create a new user in the admin panel, with the roles many-to-many. I split the name fields to first name (fname) and last name (lname). However, whenever I try to register the user, I get the error ErrorException: Undefined array key "fname". Yet, on my register page localhost:8000/register, the same technique works so perfectly.

Here's what I have in my store() method:


public function store(Request $request)
    {
        $newUser = new CreateNewUser();

        $user = $newUser->create($request->only(['name', 'email', 'gender', 'mobile_number', 'password', 'password_confirmation']))->roles()->sync($request->roles);

        // dd($user);

        return back()->with('message', 'User has been created successfully.');

    }

In my Fortify Actions CreateNewUser class, this is what I have in the create() method:


public function create(array $input)
    {
        Validator::make($input, [
            'fname' => ['required', 'string', 'max:100'],
            'lname' => ['required', 'string', 'max:100'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique(User::class),
            ],
            'password' => $this->passwordRules(),
            'gender' => ['required'],
            'mobile' => ['required', 'max:30'],
        ])->validate();

        return User::create([
            'name' => $input['fname'] . ' ' . $input['lname'],
            'email' => $input['email'],
            'gender' => $input['gender'],
            'mobile_number' => $input['mobile'],
            'password' => Hash::make($input['password']),
        ]);
    }

My inputs have the name attribute fname, lname, gender, mobile, email, password, password_confirmation. I really cannot tell why Laravel is sending me this error message. Any help regarding this is welcome. Thanks team!

0 likes
7 replies
drewdan's avatar

From what I can tell, you are doing the Validator::make passing it the input and calling validate, but then not checking to see if that validation even passed and then you try to use the array to create a user.

In your store request:

$user = $newUser->create($request->only(['name', 'email', 'gender', 'mobile_number', 'password', 'password_confirmation']))->roles()->sync($request->roles);

You are telling it to only include some data, and not passing the fname through, so the array does not have it when you try to use it in your action.

$user = $newUser->create($request->only(['name', 'fname', 'lname',  'email', 'gender', 'mobile_number', 'password', 'password_confirmation']))->roles()->sync($request->roles);

Making it include those keys should fix the issue. But I suggest you check the docs for validation here: https://laravel.com/docs/8.x/validation#manually-creating-validators and make sure you are validating correctly.

Emokores's avatar

@drewdan It is pointing out the error on the line below in the CreateNewUser Actions, specifically in the create() method:


return User::create([
       'name' => $input['fname'] . ' ' . $input['lname'],
       //
]);

I don't know why it's doing this. Yet, I would expect it to do this if maybe I did not declare the input in the frontend.

Emokores's avatar

@drewdan I have just noted that when I did dd($request), the request parameter is empty, which means the values are not being passed in the request

Emokores's avatar

@drewdan I have fixed it. I had to pass the data in the Inertia.post(route('admin.users.store'), data). But now, my array of roles is not being passed.

Emokores's avatar

@drewdan well, the manual validation works perfectly fine. The method you suggesting makes the code a lot cleaner I know. But I'm using Fortify for authentication scaffolding. However, my frontend is run by React with Inertia and my roles are dynamically displayed but not being passed in the data variable.

Emokores's avatar
Emokores
OP
Best Answer
Level 2

I have fixed this. I had to change the handleChange() event for the checkboxes to be different from the rest of the controls.

Please or to participate in this conversation.