当前位置:网站首页>Laravel 验证唯一时排除修改时的数据
Laravel 验证唯一时排除修改时的数据
2022-08-02 03:26:00 【陌潇】
laravel验证唯一性很简单:unique:user,phone 这个就是验证user表的phone字段是否唯一,但是修改数据时这样就验证过不了,怎么办呢?
use Illuminate\Support\Facades\Validator;
$msg = [
'name.required' => '姓名不能为空',
'phone.required' => '手机不能为空',
'phone.regex' => '请输入正确的手机号',
'phone.unique' => '手机号已经存在',
];
$rules = [
'name' => 'required',
//此处注意,phone后面一定要加逗号(,)不然他会把phone和id拼接,从而找不到字段报错
'phone' => 'required|regex:/^1[345789][0-9]{9}$/|unique:user,phone,'.$id,
];
$validator = Validator::make($receive, $rules, $messages);
if($validator->fails()){
$error = '';
foreach ($validator->errors()->getMessages() as $v) {
$error = $v[0];
break;
}
return sendError($error, 422);
}
这样一个验证就可以了。
边栏推荐
- A network security guinea pig's learning path - scripting of advanced usage of nmap
- [Hello World教程] 使用HBuilder和Uni-app 生成一个简单的微信小程序DEMO
- v-bind usage: class dynamic binding object array style style and function method
- Praying: 1 vulnhub walkthrough
- (4) Function, Bug, Class and Object, Encapsulation, Inheritance, Polymorphism, Copy
- The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
- DNS详解
- How to determine the direction based on two coordinate points on the map
- hackmyvm-random walkthrough
- Phonebook
猜你喜欢

web渗透必玩的靶场——DVWA靶场 1(centos8.2+phpstudy安装环境)

hackmyvm: may walkthrough

CTF-Neting Cup Past Topics

Activity

GreenOptic: 1 vulnhub walkthrough

解密:链动2+1的商业模式

The CTF introductory notes of SQL injection

考(重点理解哪些属于其他货币资金)、其他货币资金的内容、其他货币资金的账务处理(银行汇票存款、银行本票存款、信用卡存款、信用证保证金存款、存出投资款、外埠存款)

ES6 iterator explanation example

Smart Tips for Frida Scripting in Kali Environment
随机推荐
uniapp | Compilation error after updating with npm update
GreenOptic: 1 vulnhub walkthrough
(4) 函数、Bug、类与对象、封装、继承、多态、拷贝
The learning path of a network security mouse - the basic use of nmap
GreenOptic: 1 vulnhub walkthrough
什么是广告电商商业模式?这几个门派告诉你
[symfony/finder] The best file manipulation library
Scrapy crawler encounters redirection 301/302 problem solution
The shooting range that web penetration must play - DVWA shooting range 1 (centos8.2+phpstudy installation environment)
元宇宙是一个炒作的科幻概念,还是互联网发展的下半场?
链动2+1无限循环系统,2022年起盘成功率超高的模式
The CTF introductory notes of SQL injection
强化学习笔记:DDPG
Shuriken: 1 vulnhub walkthrough
hackmyvm-bunny预排
C language uses stack to calculate infix expressions
vim edit mode
(6) Design of student information management system
不懂“赚钱逻辑”,你永远都是社会最底层(广告电商)
(1) the print () function, escape character, binary and character encoding, variables, data type, the input () function, operator