Can eloquent ignore irrelevant data in Laravel 4

后端 未结 4 827
长发绾君心
长发绾君心 2021-01-27 10:33

I have a form that accepts data which will be used to create two new database table entries. The form takes both the users details and their address. The user details will be st

相关标签:
4条回答
  • You'll have to unguard that model using these http://laravel.com/docs/eloquent#mass-assignment and then manually unset those values before you execute save(). I highly recommend using a form object or something similar to complete this kind of service for you outside of your model since it's safer and usually clearer to intended behavior.

    0 讨论(0)
  • 2021-01-27 10:48

    @cheelahim is correct, When passing an array to Model::create(), all extra values that aren't in Model::fillable will be ignored.

    I would however, STRONGLY RECOMMEND that you do not pass Input::all() to a model. You really should be validating and verifying the data before throwing it into a model.

    0 讨论(0)
  • 2021-01-27 11:09

    Have you tried looking at Input::only('field1','field2',...);, or even Input::except('field3')? They should be able to accomplish what you are looking for.

    Source: http://laravel.com/docs/requests

    0 讨论(0)
  • 2021-01-27 11:12

    Sure enough you can use $fillable array in your model to declare fields allowed for mass-assignment. I believe this is the most sufficient solution in your case.

    class User extends Eloquent {
    
        protected $fillable = [
            'first_name',
            'last_name',
            'email'
        ];
    }
    
    0 讨论(0)
提交回复
热议问题