-5
A
回答
5
用戶使用相同的名字,姓氏和生日
$duplicate_user = \DB::table('users')
->where('name', 'LIKE', $new_name)
->where('surname', 'LIKE', $new_surname)
->where('birthday', '=', $new_birthday)
->first();
if($duplicate_user) {
return response('This user already exists', 422);
}
0
由於Laravel驗證Docs說
獨特:表,列,除非,idColumn
Validator::extend('and_unique', function($attribute, $value, $parameters, $validator) {
$result = DB::table($parameters[0])->where(function($query) use ($attribute, $value, $parameters) {
$query->where($attribute, '=', $value) // where name = value
->orWhere($parameters[1]), '=', $value); // where surname = value
->orWhere($parameters[2]), '=', $value); // where birthday = value
})->first();
return $result ? false : true;
});
'name' => 'required|and_unique:users,surname,birthday'
+0
這是行不通的。如果其中一個名字,姓氏或生日被採取,這將給一個錯誤。 – hatemjapo
0
$siteUser = new SiteUser;
foreach (['birth_date', 'first_name', 'last_name'] as $key) {
$siteUser->$key = $request->$key;
}
+1
你可以在你的答案中添加一些細節嗎?爲什麼OP應該使用這段代碼? – Jer
相關問題
- 1. Laravel 5檢查用戶是否登錄
- 2. laravel檢查用戶是否活動?
- 3. Laravel:檢查用戶是否有權限
- 4. 檢查用戶是否登錄laravel 4
- 5. 檢查用戶是否被禁止(Laravel)
- 6. Laravel 5.3檢查結果是否存在
- 7. Laravel s3檢查目錄是否存在。
- 8. Laravel檢查id是否存在?
- 9. Laravel 5.2檢查方法是否存在?
- 10. 檢查用戶是否存在於mysql
- 11. PHP檢查用戶是否存在PDO
- 12. 解析 - 檢查用戶是否存在
- 13. 檢查mysql用戶是否存在
- 14. Ajax檢查用戶名是否存在
- 15. 檢查Windows用戶是否存在python
- 16. php oop檢查用戶是否存在?
- 17. 檢查用戶組是否存在
- 18. 檢查用戶名是否存在PDO
- 19. Ruby websocket檢查用戶是否存在
- 20. PHP檢查用戶是否存在PDO
- 21. Node.js - 檢查用戶是否存在
- 22. 我想檢查用戶是否存在
- 23. 檢查用戶名是否存在
- 24. 檢查用戶名是否存在
- 25. 檢查用戶名是否存在| Parse.com
- 26. 檢查用戶是否存在於ou
- 27. 檢查用戶是否存在於Firestore
- 28. Laravel - 檢查帳戶是否有效
- 29. 檢查用戶名是否存在,並檢查密碼是否匹配
- 30. Swift&Firebase |使用用戶名檢查用戶是否存在
由d查看數據庫查詢以查看這些值是否在數據庫中。請參閱[查詢構建器文檔](https://laravel.com/docs/5.2/queries)。你的問題很基本,很可能會被封閉,因爲你沒有任何代碼表明你已經試圖自己做這件事。 –
laravel提供了例如'unique'來檢查數據庫中是否存在一些數據。但我不想找一個條目。 3個條目必須匹配以提供錯誤。我知道我可以做一個數據庫查詢,並檢查是否已經有一個現有的用戶,但認爲有更好的方法來做laravel – hatemjapo