2017-06-29 32 views
1

例如,如何在laravel中使用輸入驗證'email' => 'unique:users,email_address'作爲函數並返回其truefalse?我們可以嗎?
如果不能,檢查對特定列的任何輸入的最佳方法是什麼作爲函數是唯一的?如何使用輸入驗證laravel作爲函數?

+0

爲什麼不檢查文檔? https://laravel.com/docs/5.4/validation –

+3

我做了,但我現在只是從**確定如果消息存在字段**我應該使用' - > errors();'而不是使用' - > validate();'謝謝先生@BrianGlaz –

回答

0

看過documentation hereover here後我會試着舉一個例子。

use Validator; 
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 

class PostController extends Controller 
{ 

    public function store(Request $request) 
    { 
     $validator = Validator::make($request->all(), [ 
      'email' => 'unique:users,email_address', 
     ]); 

     // To get the boolean just simply use this: 
     $email_bool = $validator->passes(); 

     // $email_bool now contains a boolean testing only the unique rule of email. 
    } 
} 

你也當然只是用$validation->fails(),但是這當然會產生的$validation->passes()負。

所以基本上,你只是做一個自定義驗證規則,一個非常具體的。然後,只需在數據上對其進行測試,然後將其轉換爲具有函數的布爾值。

+0

我只知道這一點。它非常有幫助。謝謝。 –

0

我想你可以試試這個:

您還可以在控制器檢查獨特的價值,如:

$chkUniqueEmailId = DB::table('users')->where('email', '=', $request->email)->first(); 
if(!empty($chkUniqueEmailId)){ 
return redirect()->route('auth.register')->withFlashDanger("This Email address is already exists."); 
} 

希望這對你的工作!

+0

感謝您的替代方法:) –

+0

@IkhsanWisnuadjiGamadarenda你喜歡我的答案,然後請放棄投票 –