2017-05-12 57 views
0

我已经创建了驻留在AppServiceProvider中的自定义验证程序。引导方法包含一个DB方法,该方法应该接受传入验证程序的第一个patameter作为表名称。当我手动填写表格的名称,它的工作原理,但是当第一个参数传递,我碰到这个错误:如何将变量传递到自定义验证程序

QueryException in Connection.php line 729: 
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'server1.{users}' 
doesn't exist (SQL: select count(*) as aggregate from `{users}` where `email` = 
[email protected] and `STORE_ID` = 2) 

这里是我的服务提供商代码:

public function boot() 
{ 
    Validator::extend('uniqueForStore', function ($attribute, $value, $parameters, $validator) { 
     $count = DB::table($parameters[0])->where($attribute, $value)->where('STORE_ID', config('constants.STORE_ID'))->count(); 
     return $count === 0; 
    }); 
} 

这是哪里出了问题在于:

DB::table($parameters[0]) 

这里是我的注册用户表单请求代码:

public function rules() 
{ 
    return [ 
     'first_name' => 'required', 
     'last_name' => 'required', 
     'email' => "uniqueForStore:{users}", 
     'password' => 'required|min:6' 
    ]; 
} 
+1

你不能只设置“uniqueForStore:用户”? –

+0

谢谢@PatrykWoziński。有用。如果你添加答案,我可以接受它。 – andromeda

+0

好的。谢谢。 :) –

回答

1

设置你的验证规则如下 - 只是删除括号内为唯一值(用户):

public function rules() 
{ 
    return [ 
     'first_name' => 'required', 
     'last_name' => 'required', 
     'email' => "uniqueForStore:users", 
     'password' => 'required|min:6' 
    ]; 
} 
相关问题