例如,如何在laravel中使用輸入驗證'email' => 'unique:users,email_address'
作爲函數並返回其true或false?我們可以嗎?
如果不能,檢查對特定列的任何輸入的最佳方法是什麼作爲函數是唯一的?如何使用輸入驗證laravel作爲函數?
回答
看過documentation here和over 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()
負。
所以基本上,你只是做一個自定義驗證規則,一個非常具體的。然後,只需在數據上對其進行測試,然後將其轉換爲具有函數的布爾值。
我只知道這一點。它非常有幫助。謝謝。 –
我想你可以試試這個:
您還可以在控制器檢查獨特的價值,如:
$chkUniqueEmailId = DB::table('users')->where('email', '=', $request->email)->first();
if(!empty($chkUniqueEmailId)){
return redirect()->route('auth.register')->withFlashDanger("This Email address is already exists.");
}
希望這對你的工作!
感謝您的替代方法:) –
@IkhsanWisnuadjiGamadarenda你喜歡我的答案,然後請放棄投票 –
- 1. 如何驗證輸入變量作爲輸入名稱laravel?
- 2. Laravel驗證輸入數組
- 3. 如何驗證函數句柄作爲輸入參數?
- 4. 如何驗證表單輸入和laravel
- 5. javascript函數輸入驗證
- 6. 驗證輸入框,使用jquery focusin函數不起作用
- 7. Laravel輸入文件驗證
- 8. laravel中的輸入驗證?
- 9. laravel 4.1驗證URL輸入
- 10. Laravel使用哈希作爲驗證器
- 11. 輸入[MAX]爲驗證不起作用
- 12. 如何驗證輸入使用javascript
- 13. 如何使用JavaScript驗證輸入?
- 14. 驗證輸入數組引導驗證器 - Laravel 5.2
- 15. 使用laravel中的required_with進行數組輸入時的驗證?
- 16. Laravel使用數據庫驗證表單輸入
- 17. PHP laravel驗證錯誤輸入名稱作爲密鑰
- 18. Laravel JsValidator如何驗證數組輸入字段?
- 19. 如何使用數組作爲C++函數的輸入
- 20. 驗證用戶輸入爲數字,然後作爲整數使用Tryparse
- 21. 函數驗證輸入C++簡單
- 22. TypeError:undefined不是函數輸入驗證AngularJS
- 23. 驗證輸入和getline()函數
- 24. 使用Java驗證輸入
- 25. 使用JavaScript輸入驗證
- 26. 使用Jquery驗證輸入
- 27. 驗證輸入使用javascript
- 28. 使用cin驗證輸入
- 29. 使用AngularJS驗證輸入
- 30. 使用C輸入驗證
爲什麼不檢查文檔? https://laravel.com/docs/5.4/validation –
我做了,但我現在只是從**確定如果消息存在字段**我應該使用' - > errors();'而不是使用' - > validate();'謝謝先生@BrianGlaz –