【发布时间】: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,该怎么做?