duanmu3049 2017-02-22 08:55
浏览 65

Laravel 5.4验证独特的问题

I'm trying to update an user and when the email is already taken it will give an error, except when it's your own email. Right now my code is:

$user = User::find($id);

$input['email'] = request('email');

$rules = array('email' => 'unique:users,email,'.$user->id);

$validator = Validator::make($input, $rules);

if ($validator->fails()) {
    return back()->withInput()->withErrors(['email']);
}

This works perfectly fine, but now I want that it only gives an error if the email already exists AND from the found record the column 'function' = 'client', but I have no idea how to code this.

  • 写回答

3条回答 默认 最新

  • douken0530 2017-02-22 09:18
    关注
    $count = User::where('email','=',request('email'))
            ->where('function','!=','client')
            ->count();
    
    if($count){
        return back()->withInput()->withErrors(['email' => 'Email Already Exist']);
    }
    

    What will above code do :

    Count users whose email is request_email and function is not client.

    if $count is greater than 1 that means duplicate email.

    Return with error that Email Already Exist

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度