Laravel质量分配不填充字段


Laravel mass assignment won't fill fields

我有一个模型,似乎不是质量分配,即使我已经填写了$fillable字段:

class LoginAttempt extends Eloquent
{
    protected $table = 'login_history';
    protected $fillable = array('remote_addr', 'user_agent', 'successful');
    public function user()
    {
        return $this->belongsTo('User');
    }
}

哪个正在使用这个模式:

  • login_history
    • id
    • user_id
    • remote_addr
    • user_agent
    • 成功
    • created_at
    • updated_at

当我用这些变量批量赋值实例时,

$vars = array(
    'remote_addr' => $_SERVER['REMOTE_ADDR'],
    'user_agent' => $_SERVER['HTTP_USER_AGENT'],
    'successful' => false,
);
print_r($vars);
=> array('remote_addr' => '127.0.0.1', 'user_agent' => 'Moz..', 'successful' => false);
new LoginAttempt($vars);
=> LoginAttempt instance, attributes => array()
LoginAttempt::create($vars);
=> LoginAttempt instance, attributes => array()
$login = new LoginAttempt;
$login->fill($vars);
=> LoginAttempt instance, attributes => array()
$login = new LoginAttempt;
$login->remote_addr = $vars['remote_addr'];
$login->user_agent= $vars['user_agent'];
$login->successful= $vars['successful'];
=> LoginAttempt instance, attributes => array('remote_addr' => '..', 'user_agent' => '..', 'successful' => false)

我想我是用$fillable作为文档描述-为什么在这种情况下没有大规模分配工作?

原来这是Laravel的一个bug(已经修复)-所有字段都是默认保护的(protected $guarded = array('*');),然后优先于$fillable