雄辩可以忽略Laravel 4中的无关数据 [英] Can eloquent ignore irrelevant data in Laravel 4
问题描述
User :: create(Input :: all())
方法存储到users表中,地址详细信息将使用 Address :: create(Input :: all())
方法到数据库的地址表。 问题我目前来说,雄辩的是,在用户表中不存在街道,城市,国家等。这是真的,数据将用于地址方面的事情。
有没有办法让雄辩地忽略中的无关数据输入:: all()
数组,当它传递给创建方法?
Ps我知道大量分配不是一个好主意,我只是在这里使用它来简化我的问题。
p>你可以在模型中使用 $ fillable
数组来声明允许进行质量分配的字段。我相信这是你案件中最充分的解决方案。
class User Enloquent {
protected $ fillable = [
'first_name',
'last_name',
'email'
];
}
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 stored using the User::create(Input::all())
method into the users table, and the address details will be stored using the Address::create(Input::all())
method into the addresses table of the database.
The issue I'm currently having is that Eloquent is complaining that street, city, country etc do not exist on the users table. This is true, that data is to be used for the address side of things.
Is there any way to have eloquent ignore irrelevant data in the Input::all()
array when it's passed to the create methods?
P.s. I'm aware that mass-assignment isn't a good idea, I'm only using it here to simplify my question.
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'
];
}
这篇关于雄辩可以忽略Laravel 4中的无关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!