例如,如何在laravel 'email' => 'unique:users,email_address'
中使用输入验证作为函数并返回 true 或 false ?我们可以吗?
如果不能,检查特定列的任何输入的最佳方法是唯一的功能吗?
答案 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.");
}
希望这对你有用!
答案 1 :(得分:0)
在查看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()
的负数。
基本上,你只需要制作一个非常具体的自定义验证规则。然后,您只需对数据进行测试,并将其转换为带有函数的布尔值。