Variable $data might have not been defined

Is seen like I have define the $data, but how come have this error?

    public function login(Request $request){
    if($request->isMethod('post'))
        $data = $request->input();

        if (Auth::attempt(['email'=>$data['email'], 'password'=>$data['password'],'admin'=>'1']))
        {
            echo "Success";
        }
        else
        {
            echo "Failed";
        }
    return view('admin.admin_login');
}

1 answer

  • answered 2018-10-11 19:33 Felippe Duarte

    You need to put brackets after the if

    if($request->isMethod('post')) { //<--HERE
        $data = $request->input();
    
        if (Auth::attempt(['email'=>$data['email'], 'password'=>$data['password'],'admin'=>'1']))
        {
            echo "Success";
        }
        else
        {
            echo "Failed";
        }
    } //<--HERE
    

    The way your code is, you have an undefined variable $data if the condition isn't true.

    PS: You don't need to check if the request method is "post". A better way is to define in your routes if this accepted method is post or get.