laravel - Laravel 中的复合唯一验证
问题描述
如何对两列使用复合唯一验证。就像我有两列诊所名称和日期字段一样。例子。诊所, 07-2019 诊所, 08-2019
$this->validate($request, [
'clinicName'=> 'unique:hospital',
'dates'=> 'unique:hospital',
]);
我的目标是使用复合唯一验证。
解决方案
根据Laravel Docs,您可以通过以下方式定义规则来做到这一点:
添加额外的 Where 子句:
您还可以通过使用 where 方法自定义查询来指定其他查询约束。例如,让我们添加一个验证 account_id 为 1 的约束:
'email' => Rule::unique('users')->where(function ($query) { return $query->where('account_id', 1); })
因此,您可以将代码更改为:
$this->validate($request, [
'clinicName' => Rule::unique('hospital')->where(function ($query) {
return $query->where('dates', Request::get('dates'));
]);
这将检查您的clinicName
位置的唯一性dates
是否等于dates
您的请求的给定。
推荐阅读
- python - Python stringTypeError:必须是 str,而不是 NoneType
- javascript - 如何从 SVG 标签内控制 HTML 元素
- rust - 如何在这个结构定义中避免 PhantomData?
- solr - SolrCloud 的 SolrJ 客户端针对响应速度进行了优化?
- swift - IOS UILabel 在我重新加载应用程序或单击其他位置之前不会更新
- graphql - 来自 withApollo HOC 的 props.client.subscribe 的 API 文档
- php - 在 Laravel 通知电子邮件中添加粗体文本和换行符
- python - 从文件夹结构备份/复制 pip 模块
- javascript - 如何为 material-ui 组件正确使用 typescript 模块扩充?
- sql - 如何将varchar转换为日期格式