当前位置:Gxlcms > PHP教程 > laravel表单验证Exists规则的基本使用方法

laravel表单验证Exists规则的基本使用方法

时间:2021-07-01 10:21:17 帮助过:234人阅读

public function rules()
{
   return [
            'phone'       => 'required|digits_between:10,12|unique:mysql_card.operators,phone',
            'name'       => 'required',
            'market_id'       => 'required|exists:mysql_card.markets,id',
            'email' => 'required|email|unique:operators,email',
            'password' => 'required|max:20',
            'login_name' => 'required|numeric|max:10|unique:mysql_card.operators,login_name',
        ];
 }
    

以上是验证规则

'market_id'       => 'required|exists:mysql_card.markets,id',

次验证 是否存在 mysql_card 这个数据库 markets 表名是否存在 这个id

很可惜 这么写是错误的

报错如下;

QueryException in Connection.php line 636:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hi_hi_mysql_card.markets' doesn't exist (SQL: select count(*) as aggregate from `hi_hi_mysql_card`.`markets` where `id` = 1)

表前缀是 `hi_`

并没有 和 unique:mysql_card.operators,phone unique这个验证规则一样 去找我配置好的数据库

`mysql_card` 是在`config` 配置好的数据库名

exists: 验证不支持 指定数据库吗? 还是有其他方案 可以加入指定的数据库

这个是数据库配置信息 ,要验证的是第二个数据库配置信息 mysql_card

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => 'hi_',
            'strict'    => false,
        ],
'mysql_card' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => 'hi_user',
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'hi_',
    'strict'    => false,
],

回复内容:

public function rules()
{
   return [
            'phone'       => 'required|digits_between:10,12|unique:mysql_card.operators,phone',
            'name'       => 'required',
            'market_id'       => 'required|exists:mysql_card.markets,id',
            'email' => 'required|email|unique:operators,email',
            'password' => 'required|max:20',
            'login_name' => 'required|numeric|max:10|unique:mysql_card.operators,login_name',
        ];
 }
    

以上是验证规则

'market_id'       => 'required|exists:mysql_card.markets,id',

次验证 是否存在 mysql_card 这个数据库 markets 表名是否存在 这个id

很可惜 这么写是错误的

报错如下;

QueryException in Connection.php line 636:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hi_hi_mysql_card.markets' doesn't exist (SQL: select count(*) as aggregate from `hi_hi_mysql_card`.`markets` where `id` = 1)

表前缀是 `hi_`

并没有 和 unique:mysql_card.operators,phone unique这个验证规则一样 去找我配置好的数据库

`mysql_card` 是在`config` 配置好的数据库名

exists: 验证不支持 指定数据库吗? 还是有其他方案 可以加入指定的数据库

这个是数据库配置信息 ,要验证的是第二个数据库配置信息 mysql_card

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => 'hi_',
            'strict'    => false,
        ],
'mysql_card' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => 'hi_user',
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'hi_',
    'strict'    => false,
],

方法还是有的,你可以扩展laravel的验证类,自己写方法

Table 'hi_hi_mysql_card.markets' doesn't exist真的有hihimysql_card数据库和表markets吗,表前缀是 hi_?

人气教程排行