Home > Backend Development > PHP Tutorial > laravel exists怎么使用

laravel exists怎么使用

PHPz
Release: 2020-09-04 15:29:45
Original
3950 people have browsed it

laravel exists怎么使用

laravel exists怎么使用?

exists:table,column

验证字段的值是否存在于数据库指定表的指定列中,如果不存在将会抛出异常,这里是关于 exists 规则的

基本用法

验证 users 表中是否存在 email 等于指定的 email 值的记录

'email' => 'exists:users'
Rule::exists('users')
Copy after login

自定义列名

验证 users 表的 email_address

'email' => 'exists:users,email_address'
Rule::exists('users','email_address')
Copy after login

自定义数据库连接

验证指定数据库连接的 users 表

'email' => 'exists:connection.users,email'
Copy after login

附加 WHERE 条件(可以有多个)

除了验证 email_address 还需要验证 role 等于 admin

'email' => 'exists:users,email_address,role,admin'
Rule::exists('users','email_address')->where('role','admin')
Copy after login

! 表示不等于

'email' => 'exists:users,email_address,role,!admin'
Rule::exists('users','email_address')->whereNot('role','admin')
Copy after login

NULL 在 MySQL 中是通过 IS 判断的,这里注意不用 IS

'email' => 'exists:users,email,deleted_at,NULL'
Rule::exists('users','email_address')->whereNull('deleted_at')
Copy after login

对不等于 NULL 的情况不能使用 !,而是使用 NOT_NULL

'email' => 'exists:users,email,deleted_at,NOT_NULL'
Rule::exists('users','email_address')->whereNotNull('deleted_at')
Copy after login

更多相关技术知识,请访问PHP中文网

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template