我正在尝试在用户注册时将字符串(16)值设置为用户ID。 (这不会改变,必须是唯一的。它有62 ^ 16种可能性,所以我现在不担心碰撞。)
我还想设置另一个随机字符串作为激活码。当用户注册时我无法设置此项。我已经注释掉了引起麻烦的行(如果这些行已经运行,它们会被插入但是所有其他数据都被省略了。)
setters 应该在哪里?
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);
}
}
答案 0 :(得分: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);
}