validation - Laravel unique field validate not working -
i have have field called mac store mac address need unique avoid duplicate mac address.
code:
return [ 'user_id' => 'required|integer', 'mac' => array('required|unique:mac', 'regex:/^([0-9a-z]{2}[-]){5}([0-9a-z]{1,2})$/'), ];
error: method [validaterequired|unique] not exist.
structure:
create table `devices` ( `id` int(10) unsigned not null auto_increment, `user_id` int(10) unsigned not null, `name` varchar(255) collate utf8_unicode_ci not null, `mac` varchar(255) collate utf8_unicode_ci not null, `content` varchar(255) collate utf8_unicode_ci not null, `status` tinyint(1) not null default '0', `created_at` timestamp null default null, `updated_at` timestamp null default null, `deleted_at` timestamp null default null, primary key (`id`), unique key `devices_mac_unique` (`mac`), key `devices_user_id_foreign` (`user_id`), constraint `devices_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade ) engine=innodb auto_increment=3 default charset=utf8 collate=utf8_unicode_ci;
you need define table mac address need checked against
return [ 'user_id' => 'required|integer', 'mac' => 'required|unique:devices,mac|regex:/^([0-9a-z]{2}[-]){5}([0-9a-z]{1,2})$/', ];
update - when updating record, instead of create extend rule to.
return [ 'user_id' => 'required|integer', 'mac' => 'required|unique:devices,mac,' . $device->id . '|regex:/^([0-9a-z]{2}[-]){5}([0-9a-z]{1,2})$/', ];
Comments
Post a Comment