vendredi 23 février 2018

Laravel Eloquent comparing strings

I am trying to compare the strings of what is input by the user and what is stored in the database. I am checking for the email and password, and if the correct email an password are provided, then the user is redirected to the respective pages based on the role_type. I have three role types: administrator, user and staff. I am using an if statement to check for the role_types. However, the controller does not redirect me to the intended page. Any ideas why?

Here is a snippet of my code:

Routes.php

//user login
Route::get('/login', [ 'as' => 'login', 'uses' => 'mainController@login']);
Route::post('/logs_in', 'mainController@logs_in');
//user logout
Route::get('/logout', 'mainController@logout');


//dashoards
Route::get('/admin', [ 'as' => 'admin', 'uses' => 'mainController@admin']);
Route::get('/staff', [ 'as' => 'staff', 'uses' => 'staffController@staff']);
Route::get('/user', [ 'as' => 'user', 'uses' => 'userController@user']);

mainController.php

//user login
    public function login(){
        return view('login');
    }

    public function logs_in(Request $request){
        $email = $request->input('email');
        $password = $request->input('password');

        $hashedPassword = User::where('email', $email)->first();


        $role_type = User::select('role_type')->where('email', $email)-
                     >get()->first();
       // echo $role_type;
            if(Hash::check($password, $hashedPassword->password)){ 
                if($role_type == "Administrator"){ 
                    // $request->session()->put('success');
                    return redirect()->route('admin');
                } else if ($role_type == "Staff") {
                    // $request->session()->put('success');
                    return redirect()->route('staff');
                } else if ($role_type == "User") {
                    // $request->session()->put('success');
                    return redirect()->route('user');
                };
            } else {
                return redirect()->route('login')->with('login_error', 
                'Invalid credentials entered');
            };
    }


    //user logout
    public function logout() {
        Auth::logout();
        return redirect()->route('login')->with('info', 'User logged out 
        successfully');

    }

The controller is retrieving info correctly from the views. The problem lies in the redirection of the routes from /logs_in to the respective user pages.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2BLmG1m
via IFTTT

Aucun commentaire:

Enregistrer un commentaire