dongya9904 2014-07-22 01:19
浏览 11
已采纳

Laravel Mass Assignment Issue似乎不一致

I am working on implementing some Models in Laravel 4. When attempting to create a new Instance of a model, I receive a MassAssignmentException. From my research, I have learned that by default, all fields are considered guarded. In order to get this to work, I need to specify the fillable fields. Once I do that, it works fine.

However, during database seeding, I am creating some new objects using the same methods with no fillable attribute specified and I do not receive the MassAssignmentException? This is what makes no sense. Am I missing something here?

Code which threw the MassAssignmentException before I added the fillable var:

Role::create(array(
    'name' => 'admin'
));

Code in DatabaseSeeder which did not throw the Exception with no fillable var:

Company::create(array(
    'name' => 'ABC Toys Inc.'
));

Anyone know the reason for this?

  • 写回答

1条回答 默认 最新

  • dpb35161 2014-07-22 01:47
    关注

    The DatabaseSeeder class which is included in Laravel (which I assume you are using) has the Mass Assignment guarding switched off by default.

    class DatabaseSeeder extends Seeder 
    {
    
        public function run()
        {
            Eloquent::unguard();
        }
    }
    

    The Eloquent::unguard() function switches off mass assignment guarding.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制