数据未提交到数据库

时间:2019-01-26 07:08:49

标签: laravel eloquent laravel-5.7

我正在尝试注册用户,最初它正在运行,但现在不起作用,当我输入数据并单击注册时,什么也没发生,有什么解决方案可以解决此问题?

这是UsersController:

  public function register(Request $request){
    if($request->isMethod('post')){
        $data = $request->all();
        /*echo "<pre>"; print_r($data); die;*/
        // Check if User already exists
        $usersCount = User::where('email',$data['email'])->count();
        if($usersCount>0){
            return redirect()->back()->with('flash_message_error','Email already exists!');
        }else{

            $user = new User;
            $user->name = $data['name'];
            $user->email = $data['email'];
            $user->password = bcrypt($data['password']);
            $user->save();

            // Send Confirmation Email
            $email = $data['email'];
            $messageData = ['email'=>$data['email'],'name'=>$data['name'],'code'=>base64_encode($data['email'])];
            Mail::send('emails.confirmation',$messageData,function($message) use($email){
                $message->to($email)->subject('Confirm your E-com Account');
            });

            return redirect()->back()->with('flash_message_success','Please confirm your email to activate your account!');

            if(Auth::attempt(['email'=>$data['email'],'password'=>$data['password']])){
                Session::put('frontSession',$data['email']);

                if(!empty(Session::get('session_id'))){
                    $session_id = Session::get('session_id');
                    DB::table('cart')->where('session_id',$session_id)->update(['user_email' => $data['email']]);
                }

                return redirect('/cart');
            }
        }   
    }
}

这是注册表格:

  <form id="registerForm" name="registerForm" action="{{ url('/user-register') }}" method="POST">{{ csrf_field() }}
                    <input id="name" name="name" type="text" placeholder="Name"/>
                    <input id="email" name="email" type="email" placeholder="Email Address"/>
                    <input id="myPassword" name="password" type="password" placeholder="Password"/>
                    <button type="submit" class="btn btn-default">Signup</button>
                </form>

这是路线:

Route::post('/user-register','UsersController@register');

1 个答案:

答案 0 :(得分:0)

laravel中具有验证和自动登录功能的基本注册方法

public function register(Request $request)
{
     $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:6',

    ]);


    $input = $request->all();
    $input['password'] = Hash::make($input['password']);
    $user = User::create($input);
     Auth::login($user);
    return redirect()->route('dashboard')
                    ->with('success','Congratulation !!! You are registered successfully. Now you can login');
   }

通过在表格之前添加此错误,您将在刀片模板中看到什么错误

  @if($errors->all())
      @foreach ($errors->all() as $error)
        <div class="alert alert-danger">{{ $error }}</div>
      @endforeach
  @endif