Laravel Auth :: try () returns true, but Auth :: check () returns false

As the name suggests, the Laravel function Auth::attempt()

returns true

in the following section of code (with minor details):

public function doLogin()
{
    $validator = [..]

    if ($validator->fails()) {
        [..]
    } else {
        $userdata = array(
            'username'  => Input::get('username'),
            'password'  => Input::get('password')
        );

        if (Auth::attempt($userdata, true)) {
            return Redirect::to('/');
        } else {        
            return Redirect::to('login');
        }
    }
}

      

But when we are redirected, we try to check if the user is actually logged in with Auth::check()

, and it somehow returns false

.

We have tried every possible solution at Google and have had no success with any of them. For example, we added remember_token

, but did not change anything, although we proved Auth::attempt()

that it does something because it is installed in the database remember_token

.

As a last resort, we even tried to print something in Auth::attempt()

Laravel's method ./vendor/laravel/framework/src/Illuminate/Auth/Guard.php

, but we didn't see anything, not even with print_r

. We tried to find other feature attempts in the full codebase, but none were found.

Maybe something about the change User

in the translated form makes it broken, but then the function Auth::attempt()

should be broken as well, maybe.

It seems like something really magical is happening, but we have no idea what or how. Anyone else have an idea?

+3


source to share


1 answer


By default, Laravel assumes that each table has a primary key named id.

A possible solution is to do this in your model:



protected $primaryKey = 'id'; //Or what ever id name do you have.

      

This error can occur if you are trying to use laravel-mongodb with a custom increment and unique generated id, not the default _id from MongoDB. The solution in this case is not to specify your custom id like this "id" because that would make Laravel confusing. However, name it something else.

+4


source







All Articles