2017-05-12 51 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' 
    ]; 
} 
相關問題