Laravel质量分配不会填补字段

时间:2013-06-06 14:48:02

标签: php laravel laravel-4 mass-assignment

即使我填写了$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作为文档描述 - 为什么在这种情况下批量作业不起作用?

1 个答案:

答案 0 :(得分:4)

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