雄辩可以忽略Laravel 4中的无关数据 [英] Can eloquent ignore irrelevant data in Laravel 4

查看:122
本文介绍了雄辩可以忽略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屋!

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