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_
?