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

laravel exists怎么使用

PHPz
PHPzOriginal
2016-06-06 20:29:553948browse

laravel exists怎么使用

laravel exists怎么使用?

exists:table,column

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

基本用法

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

'email' => 'exists:users'
Rule::exists('users')

自定义列名

验证 users 表的 email_address

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

自定义数据库连接

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

'email' => 'exists:connection.users,email'

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

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

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

! 表示不等于

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

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

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

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

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

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

Statement:
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