Laravel质量分配不会填满场(Laravel mass assignment won't

2019-10-18 05:25发布

我有这似乎并不为大众分配,即使我已经填补了一个模型$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
    • 用户身份
    • REMOTE_ADDR
    • 用户代理
    • 成功
    • 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的文档描述-为什么不是质量分配在这种情况下工作?

Answer 1:

事实证明这是一个错误的Laravel(已定 ) -所有字段默认守卫( protected $guarded = array('*'); ),然后优先于$fillable



文章来源: Laravel mass assignment won't fill fields