0

在我的应用程序中,我有一个请求文件如下:

storeFactoryUser.php

public function rules()
    {
        return [
            'factory_users' => 'array',
            'factory_users.*.first_name' => 'required',
            'factory_users.*.last_name' => 'required',
            'factory_users.*.username' => 'required|alpha_dash|unique:users,username',
            'factory_users.*.f_user_email' => 'required|unique:users,email',
            'factory_users.*.external_ref' => 'required'
        ];
    }

现在,当我输入一个已经存在的用户名时,它会返回如下错误消息:

The factory_users.1.username has already been taken.

我需要用数组值来显示它,而不是像上面那样显示它,例如:

Given factory user's username factory1 has already been taken.

为此,我编写了一个消息函数,如下所示:

public function messages()
{
    $messages = [];
    foreach ($this->factory_users as $key => $factory_user) {
        $messages['factory_users.*.username.alpha_dash|unique:users,username'] = "Given factory user's username ".$factory_user['username'].' has already been taken.';
    }

    return $messages;
}

但是,它仍然返回我上面提到的相同错误消息。

4

1 回答 1

0

你应该这样使用..

$factory_user['username']

代替

$factory_user->username
于 2021-01-18T07:19:43.553 回答