猿问

如果用户 emal 重复,我如何捕获并返回 json

如果数据库中有重复的用户电子邮件,我想返回响应


我已经尝试使用子句“where”检查电子邮件,但返回的是 views/welcome.blade.php


我希望json响应为{成功:错误,错误:“电子邮件已注册”}


public function register(Request $request)

{

    // VALIDATION FIELD

    $this->validate($request, [

        'name'          => 'required',

        'company_name'  => 'required',

        'email'         => 'required|email|max:255|unique:users',

        'password'      => 'required|min:8'

    ]);


    try {


        // DATA REQUEST

        $data = [

            'name'          => $request->input('name'),

            'company_name'  => $request->input('company_name'),

            'email'         => $request->input('email'),

            'password'      => Hash::make($request->input('password'))

        ];


        User::create($data);


        // IF SUCCESS RETURN SUCCESS => TRUE

        return response()->json([

            'success' => true,

            'data'  => $data

        ]);


        if(User::where('email', '=', $data->email )->exists()){

            return response()->json([

                'success' => false,

                'error'  => 'Email already registered, try another email'

            ]);

        }else{

            // CREATE DATA

            User::create($data);


            // IF SUCCESS RETURN SUCCESS => TRUE

            return response()->json([

                'success' => true,

                'data'  => $data

            ]);

        }



    }catch (\Throwable $e) {

        // IF SUCCESS RETURN SUCCESS => False

        return response()->json([

            'success'   => false,

            'error'     => "Error On Line {$e->getLine()}, <br>Message:<br>{$e->getMessage()}",

        ]);

    }

}



慕慕森
浏览 144回答 1
1回答

宝慕林4294392

拉维尔5:使用以下手动验证过程:&nbsp; &nbsp; &nbsp; &nbsp; // use Validator facade after namespace & replace below line of code&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //$this->validate($request, [&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; 'email'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'required|email|max:255|unique:users'&nbsp; &nbsp; &nbsp; &nbsp; //]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $validator = Validator::make($request->all(), [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'required|email|max:255|unique:users'&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; if ($validator->fails()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $errors = $validator->errors();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($errors->has('email')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Return json response with first error.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return response()->json([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'success' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'error'&nbsp; &nbsp;=> $errors->first('email')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }访问更多参考:Laravel Doc
随时随地看视频慕课网APP
我要回答