Laravel 5.5 unique validation rule on seperate table with different column name(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 对具有不同列名的单独表的唯一验证规则
基础教程推荐
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
