简体   繁体   English

Laravel质量分配不会填补字段

[英]Laravel mass assignment won't fill fields

I have a model that doesn't seem to be mass assignable, even though I have filled in the $fillable 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');
    }
}

Which is using this schema: 哪个使用此架构:

  • login_history login_history
    • id ID
    • user_id 用户身份
    • remote_addr REMOTE_ADDR
    • user_agent 用户代理
    • successful 成功
    • created_at created_at
    • updated_at 的updated_at

When I mass assign the instance with these variables, 当我使用这些变量批量分配实例时,

$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)

I think I'm using $fillable as the docs describe - why isn't mass assignment working in this case? 我认为我正在使用$fillable作为文档描述 - 为什么在这种情况下批量作业不起作用?

原来这是Laravel中的一个错误 (已修复 ) - 默认情况下所有字段都被保护( protected $guarded = array('*'); )然后优先于$fillable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM