Support the ongoing development of Laravel.io →
Database Eloquent

I get this error when I logout (http://example.com/logout). I click on a link which takes me to the route named 'logout' and I get this error.

I keep track of all of the IDs used throughout my application (MDBIDs). They are alphanumeric strings of length 16.

I insert the user's MDBID into the mdbids table when the user is created (registered) however, it appears that the store() method is called again or at least something similar as I get this error stating that "Integrity constraint violation: 1062 Duplicate entry"

Any ideas how to fix it?

screenshot

User.php (model)

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
/**
 * @var string
 */
protected $primaryKey = 'mdbid';
/**
 * @var bool
 */
public $incrementing = false;

// Don't forget to fill this array
/**
 * @var array
 */
protected $fillable = array('name', 'dob', 'email', 'username', 'password');

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

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken()
{
    return $this->remember_token;
}

/**
 * Set the token value for the "remember me" session.
 *
 * @param  string  $value
 * @return void
 */
public function setRememberToken($value)
{
    $this->remember_token = $value;
}

/**
 * Get the column name for the "remember me" token.
 *
 * @return string
 */
public function getRememberTokenName()
{
    return 'remember_token';
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

public function setPasswordAttribute($password)
{
    $this->attributes['password'] = Hash::make($password);
}

public function save(array $options = array())
{
    $this->mdbid = $this->mdbid ?: str_random(16);
    $this->key  = $this->key  ?: str_random(11);

    // This causes an integrity constraint when I logout
    Mdbid::firstOrCreate([
        'mdbid'        => $this->mdbid,
        'table_number' => 7,
        'table_name'   => 'users',
        'created_at'   => Carbon::now(),
        'updated_at'   => Carbon::now()
    ]);

    parent::save($options);
    //static::saveMdbid($this->mdbid);
}




/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function tags()
{
    return $this->belongsToMany('Tag', 'tag_track_user', 'user_mdbid', 'tag_mdbid')->withPivot('track_mdbid');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function tracks()
{
    return $this->belongsToMany('Track', 'tag_track_user', 'user_mdbid', 'track_mdbid')->withPivot('tag_mdbid');
}

public function profile()
{
    return $this->hasOne('Profile', 'user_mdbid', 'mdbid');
}

}
Last updated 2 years ago.
0

When you logout, I think laravel updates the remember_token field of your users table. If you have some kind of listeners on the saved event to deal with your id logic, you could try moving them to a created event instead and see if that fixes it.

Last updated 2 years ago.
0

Where would I check this? I don't have any event listeners that I am aware of. I did at one point which just logged the SQL queries but nothing else now.

Last updated 2 years ago.
0

Looking at your error stack, your user gets saved due to the remember_token and then firstOrCreate gets triggered. I'm not sure where you would have listeners set or what is going on with your code but you need to figure out why the firstOrCreate stuff is getting triggered on saves.

Last updated 2 years ago.
0

did you get any further with this?

Getting this error intermittently from mobile devices..

0

Sign in to participate in this thread!

Eventy

Your banner here too?

mstnorris mstnorris Joined 12 Mar 2014

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.

© 2025 Laravel.io - All rights reserved.