user10354117
user10354117

Reputation: 71

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');
}

Upvotes: 0

Views: 54

Answers (1)

Felippe Duarte
Felippe Duarte

Reputation: 15131

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.

Upvotes: 4

Related Questions