使用whereRaw查不到数据

//查询到本用户
        $currentUser = Auth::User();//User::where('id', '=', Auth::User()->id)->first();
        //检查绑定的邮箱除自己已经设置的以外是否存在
        $otherUsersCount = User::whereRaw("`id`!=? and `email`='?'",[$currentUser->id,$currentUser->email])->count();
        if( $otherUsersCount > 0 ){

            return response()->json(['status'=>'error', 'messages'=>'该邮箱已经被使用!']);

        }

但count不管怎么查,永远都是0,怎么破....
已邀请:

Mr_Jing

赞同来自: KurobaTouichi

你既然都使用了 whereRaw,就应该在 SQL 中写 select count(*) from ……,而不是再后来用 ->count() 方法。还有,如果是很普通的SQL,就没有必要用 whereRaw 了。
你那句就直接写 ->where('id', '!=', $currentUser->id)->where('email', $currentUser->email)->count() 就可以了。

还有,根据你这个场景,其实你就是想判断邮箱是不是已经存在了,其实可以使用 Validation 中的 Exists 规则,http://www.golaravel.com/larav ... xists

要回复问题请先登录注册