【问题标题】:Laravel 8 Validation rule not working for unique email error always returning falseLaravel 8 验证规则不适用于唯一的电子邮件错误总是返回 false
【发布时间】:2022-12-17 19:08:05
【问题描述】:

我需要在电子邮件唯一的地方插入客户数据。这是mysql

CREATE TABLE IF NOT EXISTS `customers` (
  `id` int NOT NULL AUTO_INCREMENT,
  `customer_name` varchar(20) NOT NULL,
  `customer_address` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `customer_email` varchar(20) NOT NULL,
  `customer_city` varchar(50) NOT NULL,
  `post_code` varchar(20) NOT NULL,
  `customer_phone` int NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `customer_email` (`customer_email`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
COMMIT;

这里的客户模型:

class Customer extends Model
{
    protected $table="customers";
    protected $fillable = ['customer_name', 'customer_email','customer_address', 'customer_city','customer_postcode'];
 }

在控制器中:

 $validator = Validator::make($request->all(),
            [
            'customer_name' => 'required',
            'customer_address' => 'required',
            'customer_email' => "required|unique:customers",
            'customer_city' => 'required',
            'post_code' => 'required',
            'customer_phone' => 'required',
        ]);

            var_dump($validator->fails());

$validator->fails() 总是返回 false。我缺少什么?提前致谢。

【问题讨论】:

  • 检查POST请求包含customer_email
  • 你能转储 $request->all() 这样我们就可以看到正在验证的内容吗?
  • + 更新payload时需要跳过当前ID。
  • 这是转储数组( [customer_name] => Muntashir Are Rahi [customer_address] => Dhaka, 726/25/A [customer_city] => Dhaka [post_code] => 1207 [customer_email] => muntashir.rahi@gmail.com [customer_phone] => 01712552303 )
  • @AbdullaNilam,该怎么做?

标签: mysql laravel eloquent


【解决方案1】:

看到这个解决方案我想你会找到你想要的。 https://laracasts.com/discuss/channels/laravel/unique-email-not-working-on-validator

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 2020-05-26
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2019-12-25
    • 2016-11-02
    相关资源
    最近更新 更多