【问题标题】:Laravel 5.5 validate unique email with model custom idLaravel 5.5 使用模型自定义 ID 验证唯一电子邮件
【发布时间】:2017-11-21 09:16:29
【问题描述】:

如何验证模型具有自定义 ID 的唯一电子邮件。这是我的更新验证规则

'email' => 'required|unique:accounts,email,' . $user->account_id,

但这会失败并出现 sql 错误 Unknown column 'id' in 'where clause'

id 应该是account_id

这是尝试的查询

select count(*) as aggregate from `accounts` where `email` = test@test.com and `id` <> 2445

【问题讨论】:

  • 出了什么问题?
  • 在帖子中突出显示Unknown column 'id' in 'where clause' id 应该是 account_id
  • $user->account_id 的值是多少

标签: validation laravel-5 model unique


【解决方案1】:

进行了一些研究,我发现以下内容可以验证自定义索引作为规则的独特要求

'email' => ['required',
        Rule::unique('accounts')->ignore($user->account_id, 'account_id')
]

【讨论】:

    【解决方案2】:
    'email' => 'required|unique:accounts,email,'.$user->account_id.',my_custom_user_id'
    

    指定您要更改的表的 ID。它通常是一个自定义 ID。

    【讨论】:

      猜你喜欢
      • 2011-12-08
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 2015-03-24
      • 2014-06-08
      • 1970-01-01
      相关资源
      最近更新 更多