Question

I am trying to set a string(16) value as the User ID upon user registration. (This will not change and must be unique. It has 62^16 possibilities so I am not worried about collision at this point.)

I also want to set another random string to act as an activation code. I am having trouble with setting this when the user signs up. I have commented out the lines that are causing trouble (if these are left to run, they are inserted but all the other data is omitted).

Is this where the setters should go?

User.php (model)

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface {


//    public function __construct()
//    {
//        $this->attributes['mdbid'] = str_random(16);
//        $this->attributes['key'] = str_random(11);
//    }

/**
 * @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);
}

}

Was it helpful?

Solution

I would probably override the save() method:

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

    $this->key  = $this->key  ?: str_random(11);

    parent::save($options);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top