沃梦达 / 编程问答 / php问题 / 正文

Laravel 5.5 对具有不同列名的单独表的唯一验证规则

Laravel 5.5 unique validation rule on seperate table with different column name(Laravel 5.5 对具有不同列名的单独表的唯一验证规则)

本文介绍了Laravel 5.5 对具有不同列名的单独表的唯一验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有用户和公司.一个用户属于一个公司.

So I have users and companies. A user belongs to one company.

我想验证用户注册,以便他们用来注册的 business_name 字段在 companys 表中是唯一的,目标是不允许用户创建重复公司.

I want to validate a user registration so that the business_name field they use to register is unique in the companies table, the goal is to not allow users from creating duplicate companies.

这是我的注册函数:

public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'first_name' => 'required',
            'last_name' => 'required',
            'business_name' => 'required|unique:companies',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6',
        ]);


        if ($validator->fails()) {
            return response()->json(['error'=>$validator->messages()], 401);
        }

}

我要比较的字段是用于检查唯一性的 companies.name.

The field I want to compare against is the companies.name to check for uniqueness.

这可能吗?目前它正在尝试在 companys 表中查找 business_name.

Is this possible? At the moment it is trying to look for business_name in the companies table.

推荐答案

没关系,设法弄明白了.只需要一个额外的参数来指定列名:

Never mind, managed to figure it out. Just needed an extra parameter to specify the column name:

'business_name' => 'required|unique:companies,name',

这篇关于Laravel 5.5 对具有不同列名的单独表的唯一验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 5.5 对具有不同列名的单独表的唯一验证规则

基础教程推荐