当前位置:网站首页>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);
}
这样一个验证就可以了。
边栏推荐
- 学IT,找工作——移除链表元素
- How to log in to Alibaba Cloud server using the admin account
- 不懂“赚钱逻辑”,你永远都是社会最底层(广告电商)
- Orasi: 1 vulnhub walkthrough
- Praying: 1 vulnhub walkthrough
- VIKINGS: 1 vulnhub walkthrough
- How to determine the direction based on two coordinate points on the map
- (7) superficial "crawlers" process (concept + practice)
- GreenOptic: 1 vulnhub walkthrough
- 链动2+1无限循环系统,2022年起盘成功率超高的模式
猜你喜欢
laravel 查询数据库获取结果如何判断是否为空?
The learning path of a network security mouse - the basic use of nmap
hackmyvm-random walkthrough
web渗透必玩的靶场——DVWA靶场 1(centos8.2+phpstudy安装环境)
链动2+1无限循环系统,2022年起盘成功率超高的模式
PHP deserialization vulnerability
账务处理程序、记账凭证账务处理程序、汇总记账凭证账务处理程序、科目汇总表账务处理程序、会计信息化概述、信息化环境下会计账务处理的基本要求(此章出1道小题)
File upload vulnerability
c语言用栈实现计算中缀表达式
链动2+1模式开发系统
随机推荐
Masashi: 1 vulnhub walkthrough
【泰山众筹】模式为什么一直都这么火热?是有原因的
CTF-网鼎杯往届题目
What are the killer super powerful frameworks or libraries or applications for PHP?
3000字入门图神经网络
hackmyvm: kitty walkthrough
英语每日打卡
如何一步一步的:玩转全民拼购!
Praying: 1 vulnhub walkthrough
CSRF(跨站请求伪造)
Orasi: 1 vulnhub walkthrough
Phonebook
敏感信息泄露
Several interesting ways to open PHP: from basic to perverted
PHP Foundation March Press Announcement Released
(1) introduction to Thinkphp6, installation view, template rendering, variable assignment
[symfony/finder] The best file manipulation library
17. JS conditional statements and loops, and data type conversion
file contains vulnerabilities
Scrapy爬虫遇见重定向301/302问题解决方法