可以在 Laravel 4 中 eloquent 忽略不相关的数据 [英] Can eloquent ignore irrelevant data in Laravel 4

查看:22
本文介绍了可以在 Laravel 4 中 eloquent 忽略不相关的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受数据的表单,这些数据将用于创建两个新的数据库表条目.该表单包含用户详细信息和他们的地址.用户详细信息将使用 User::create(Input::all()) 方法存储到用户表中,地址详细信息将使用 Address::create(Input::all()) 方法输入到数据库的地址表中.

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.

我目前遇到的问题是 Eloquent 抱怨用户表中不存在街道、城市、国家等.确实如此,该数据将用于事物的地址方面.

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.

有没有什么办法可以让 eloquent 在将 Input::all() 数组中的无关数据传递给 create 方法时忽略它?

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.

推荐答案

果然可以在模型中使用 $fillable 数组来声明允许批量赋值的字段.我相信这是最充分的解决方案.

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 中 eloquent 忽略不相关的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆