Support the ongoing development of Laravel.io →
Authentication Session Views
Last updated 2 years ago.
0
Solution

Problem Solved:

The main problem is that my user table id is customized which is not a auto_increment id.

After adding public $incrementing = false; to my user model. The problem solved.

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;


    public $incrementing = false;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'register_info';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id','username', 'email', 'password','city'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';

    /**
     * Get the bonding UserDetail
     *
     * @return UserDetail
     */
    public function getUserDetail()
    {
        return $this->hasOne('App\Model\UserDetail','user_id','user_id');
    }

}

Problem analysis: In laravel, by default the id of model user is set as auto-increment. If we use customized user_id like i do.

 /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {

        return User::create([
            'user_id' => uniqid('us'),
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password'])
        ]);
    }

The returned model will contain a auto-increment id. What i got is always 0. Therefore, a User instance with id 0 is stored in laravel. When i call Auth:user(), we will get null instead of the User instance.

{"user_id":"0","username":"[email protected]","password":"xxxxxxx"}

After adding public $incrementing = false; to my user model. I can get my user instance correctly with id that i defined.

{"user_id":"us55f7e7afbe87f","username":"[email protected]","password":"xxxxxxx"}

Hope this can be helpful.

Thanks everyone helped before.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

OSCAR oscar Joined 15 Sep 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.