Laravel-忽略使用model :: create()时不存在的字段 [英] Laravel - Ignoring fields that don't exist when using model::create()

查看:957
本文介绍了Laravel-忽略使用model :: create()时不存在的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



当Eloquent的<$ c存在时,我不能让Laravel忽略数据库中不存在的字段$ c> create 方法或在为数据库添加种子时。在下面的示例中,日期字段在事件表中不存在。但是,我需要它存在于数组中,以便可以在EventObserver.php中使用它,以便在创建$ event的同时创建$ event-> day。我意识到我可以在自己的工厂中创建 Day ,我只想知道这是否有可能甚至可取。



错误


未找到列:1054字段列表中的天未知列


我的代码



我正在尝试将Observer方法附加到我的Event模型中,该方法侦听创建的方法,以便为每个关系创建一个新对象。在我的EventObserver中,我具有以下内容:

 创建的公共函数(事件$ event)
{

//获取所有属性
$ data = $ event-> getAttributes();

#填充任何相关模型
$ event-> day-> fill($ data [’day’]);

#保存事件
$ event-> push();
}

在我的事件工厂中,我有这个;

  // EventFactory.php 
$ factory-> define(App\Event :: class,function(Faker $ faker){
return [
name => A Test Event,
description = >> $ faker-> paragraphs(3,true),
event_start_date = > today(),
event_opening_date => today(),
event_closing_date =>明日(),
user_id => 1,
banner_id => 1,
gallery_id => 1,
related_event_id => 1,
status => published,
purchase_limit => 1000,
limit_remaining => 1000,
delivery_method =>收藏,
商品_delivery_method => collection,
day => [
event_id => 1,
名称 => 由观察者创建,
日期 => today(),
ticket_limit => 1000,
limit_remaining => 1000
]
];
});

摘要



如何在创建事件时使Laravel忽略 day:[] 属性,以便我仍然可以在Observer中使用它?

解决方案

Eloquent不存储表中存在哪些列。因此,它将尝试将您传递的所有字段插入到create方法中,只要它们未被批量分配保护阻止即可。



一种避免这种情况的方法是在模型的 $ fillable 属性中显式声明所有字段。模型上的质量分配保护将过滤出$ fillable中不存在的所有字段。



在这种情况下,您应该永远不要创建天索引,因为这会导致观察者无法正常工作。



在您的EventFactory中。为什么要创建模型工厂中不存在的索引?创建相关数据的方法要好得多,例如为相关模型创建和使用其他工厂。



您还可以创建自己的方法来创建和填充模型和相关数据。我不会尝试覆盖默认的创建功能。


The Problem

I can't make Laravel ignore fields that don't exist in the database when present in Eloquent's create method or when seeding the database. In the example below, the "day" field does not exist in the Event table. However, I need it present in the array so I can use it in the EventObserver.php in order to create an $event->day at the same time as creating an $event. I realise I could create the Day in it's own factory, I just want to know if this is possible or even advisable.

The error

Column not found: 1054 Unknown column 'day' in 'field list'

My Code

I am trying to attach an Observer method to my Event model that listens to the created method so I can create a new object for each relationship. In my EventObserver, I have the following;

public function created(Event $event)
{

//get all attributes
$data = $event->getAttributes();

# fill any related models
$event->day->fill($data['day']);

# save event
$event->push();
}

In my Event Factory, I have this;

//EventFactory.php
$factory->define(App\Event::class, function (Faker $faker) {
    return [
        "name"                        => "A Test Event",
        "description"                 => $faker->paragraphs(3, true),
        "event_start_date"            => today(),
        "event_opening_date"          => today(),
        "event_closing_date"          => tomorrow(),
        "user_id"                     => 1,
        "banner_id"                   => 1,
        "gallery_id"                  => 1,
        "related_event_id"            => 1,
        "status"                      => "published",
        "purchase_limit"              => 1000,
        "limit_remaining"             => 1000,
        "delivery_method"             => "collection",
        "merchandise_delivery_method" => "collection",
        "day"                         => [
            "event_id"        => 1,
            "name"            => "Created with observer",
            "date"            => today(),
            "ticket_limit"    => 1000,
            "limit_remaining" => 1000
        ]
    ];
});

Summary

How can I make Laravel ignore the day: [] property when creating an Event so I can still use it in the Observer?

解决方案

Eloquent doesn't store which columns exist in a table. Therefore, it will try to insert any fields you pass into the create method as long as they aren't blocked by the mass assignment protection.

One way to avoid this is to explicitly declare all fields in the $fillable property on your model. The mass assignment protection on the model will filter out all fields that don't exist in $fillable. This would prevent your observer from working properly since 'day' won't exist in the Model attributes.

In your case, you should never be creating the 'day' index in your EventFactory. Why would you create an index that doesn't exist in the factory for a model? There are much better ways to create related data, such as creating and using a different factory for the related models.

You can also create your own methods for creating and filling the model and related data. I wouldn't try to override the default create functionality.

这篇关于Laravel-忽略使用model :: create()时不存在的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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