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

iamamirsalehi's avatar

array_map(): Expected parameter 2 to be an array, int given

Hi everyone!

When i send my data to store method i get the above error. this's my code in store method

$data = $request->validate([
            'nationalCode' => ['required', 'regex:/^[0-9]{10}$/', 'unique:users,nationalCode'],
            'username'     => "required|min:3|max:40|unique:users,username",
            'email'        => ['regex:/[^@]+@[^\.]+\..+/', "unique:users,email"],
            'mobile'       => ['required','regex:/09(1[0-9]|3[1-9]|2[1-9])-?[0-9]{3}-?[0-9]{4}/',],
            'firstName'    => 'required|min:3|max:40',
            'lastName'     => 'required|min:3|max:40',
            'province'     => 'required',
            'role'         => 2,
            'isActive'     => 1,
        ]);

        // generate password automatically 
        $password =  bcrypt('password_star');

        $data += ['password' => $password];

        User::create($data);

        alert()->success('مدبر مورد نظر با موفقیت ایجاد شد', 'موفقیت آمیز');

        return redirect(route('admin.user.index'));

what should i do?

0 likes
10 replies
ajithlal's avatar

I think the issue is with the assigning the password. Since $data is having an array of values, you can add password to array like below.

$data = $request->validate([
            'nationalCode' => ['required', 'regex:/^[0-9]{10}$/', 'unique:users,nationalCode'],
            'username'     => "required|min:3|max:40|unique:users,username",
            'email'        => ['regex:/[^@]+@[^\.]+\..+/', "unique:users,email"],
            'mobile'       => ['required','regex:/09(1[0-9]|3[1-9]|2[1-9])-?[0-9]{3}-?[0-9]{4}/',],
            'firstName'    => 'required|min:3|max:40',
            'lastName'     => 'required|min:3|max:40',
            'province'     => 'required',
            'role'         => 2,
            'isActive'     => 1,
        ]);

        // generate password automatically 
        $data['password'] =  bcrypt('password_star');

        User::create($data);

        alert()->success('مدبر مورد نظر با موفقیت ایجاد شد', 'موفقیت آمیز');

        return redirect(route('admin.user.index'));
iamamirsalehi's avatar

I think that's not the problem but i tried what you said, unfortunately it doesn't work

Sinnbeck's avatar

Where is the error thrown? Also check the callstack and see if you can see where the error comes from (it should give you a line number)

Sinnbeck's avatar

Does it work if you remove these two?

            'role'         => 2,
            'isActive'     => 1,
iamamirsalehi's avatar
[2020-08-24 08:23:36] local.ERROR: array_map(): Expected parameter 2 to be an array, int given {"userId":1,"exception":"[object] (ErrorException(code: 0): array_map(): Expected parameter 2 to be an array, int given at /home/isamirsalehi/projects/tarh/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php:92)
[stacktrace]
#12 /home/isamirsalehi/projects/tarh/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php(111): call_user_func_array()
#13 /home/isamirsalehi/projects/tarh/app/Http/Controllers/Admin/User/AdminController.php(41): Illuminate\Http\Request->__call()
#14 [internal function]: App\Http\Controllers\Admin\User\AdminController->store()

line 41 is the first validation that is nationalCode but when i delete it the error still exists

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Ok then that would be the cause (It cannot use an integer)

This is the code

if (is_string($rule)) { //2 isnt a string
            return explode('|', $rule);
        } elseif (is_object($rule)) { //2 isnt an object
            return [$this->prepareRule($rule)];
        }

return array_map([$this, 'prepareRule'], $rule); //rule here is an integer = fail

Add them afterwards instead

$data['role'] =  2;
$data['isActive'] =  1;
1 like
Sinnbeck's avatar

You are passing invalid data. That means laravel cannot handle it and throws an error. I gave you a solution above.

Please or to participate in this conversation.