php - laravel 表单验证 Exists 规则的基本使用方法
怪我咯
怪我咯 2017-04-10 15:55:27
0
2
421
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,phoneunique这个验证规则一样 去找我配置好的数据库

`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, ],

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all (2)
迷茫

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

    Ty80

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

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!