2014-05-10 34 views
0

我想在用户注册时设置一个字符串(16)值作为用户ID。 (这不会改变,必须是唯一的,它有62^16个可能性,所以我不担心碰撞在这一点上。)如何在laravel中注册用户时设置初始值?

我也想设置另一个随机字符串作为激活码。我在用户注册时遇到了麻烦。我已经注释掉了引起麻烦的线路(如果这些线路还在运行,它们将被插入,但所有其他数据都将被忽略)。

这是安装员应该去哪里?

user.php的(模型)

<?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); 
} 

}

回答

1

我可能会覆盖save()方法:

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

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

    parent::save($options); 
} 
+0

太谢谢你了。我选择了后者。第一个没有工作:) – Mike

相关问题