Laravel 创建自定义消息时,自定义请求未捕获规则中的唯一性

Laravel Custom Request not catching the unique in the rules when creating custom messages

我已经阅读了 12 遍文档并创建了以下内容:

class LSDLocationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'organization_id'  => 'required',
            'name'             => 'required|unique:lsd_locations'
        ];
    }

    public function messages()
    {
        return [
            'name.unique:lsd_locations' => 'The LSD name is already used by a LSD Location.',
        ];
    }
}

在我的控制器中:

public function store(LSDLocationRequest $request, Organization $organization) {
    $request->validated();

    // ...
}

当我第一次提交这个名称为 Sample Location 的表单时,它起作用了。当我第二次使用相同的名称时,它显示:

The name has already been taken.

我 110% 确定这是它进入的正确控制器动作。为什么它不显示我的自定义消息?我在 table 中的名称字段上有一个唯一键。它忽略了我的 name.unique:lsd_locations 并编写了默认消息。

为什么不使用我的消息?为什么使用默认消息?

只需将消息更改为:

name.unique

所以应该是这样的:

public function messages()
{
    return [
        'name.unique' => 'The LSD name is already used by a LSD Location.',
    ];
}