php 在 eloquent / Laravel 上保存时发现意外数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23042630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:26:55  来源:igfitidea点击:

Unexpected data found during save on eloquent / Laravel

phplaravel-4eloquentphp-carbon

提问by Fabrizio Fenoglio

I have one more field on the database over the created_atand updated_atas TIMESTAMPthe field name is date.

我在数据库上还有一个字段created_atupdated_at因为TIMESTAMP字段名称是日期。

So i overwritten the method getDates()on my model eloquent because i wanted that field be instantiated from Carbon.

所以我getDates()在我的模型上覆盖了eloquent的方法,因为我希望从 Carbon 实例化该字段。

public function getDates()
{
   return ['date','created_at','updated_at'];
}

But when i go to create a new record on the database it throw me an exception:

但是当我在数据库上创建一个新记录时,它抛出了一个异常:

InvalidArgumentException Unexpected data found. Unexpected data found. Unexpected data found.

InvalidArgumentException 发现意外数据。发现意外数据。发现意外数据。

Ps: the value sent from the form is in EU format: d-m-Y h:i

Ps:表单发送的值是EU格式的: d-m-Y h:i

I don't know how figure out this problem any suggestion are appreciated

我不知道如何解决这个问题任何建议表示赞赏

采纳答案by Jarek Tkaczyk

You array returned from getDates was merged with the dafault one resulting in:

您从 getDates 返回的数组与 dafault 合并,导致:

['created_at','updated_at','deleted_at','date','created_at','updated_at'];

so use only 'date' there and should be fine.

所以只在那里使用“日期”,应该没问题。



Try setting up a mutator for 'date' to convert the data from input into timestamp format. The error you get is not on Eloquent but Carbon.

尝试为 'date' 设置一个 mutator,以将数据从输入转换为时间戳格式。你得到的错误不是 Eloquent 而是 Carbon。

public function setDateAttribute($value)
{
    $this->attributes['date'] = Carbon\Carbon::createFromFormat('d-m-Y h:i', $value);
}

Also there is mistake in the docs, as getDates defines date accessors, not mutators..

文档中也有错误,因为 getDates 定义了日期访问器,而不是修改器..

回答by Do?uhan Elma

Try this:

尝试这个:

Carbon::createFromFormat('d.m.Y H:i', $request->publishdate);    

回答by Mantas D

You can't use your format "d-m-Y h:i"

你不能使用你的格式“dmY h:i”

You must use one of those: UNIX timestamp, date string (Y-m-d), date-time string, DateTime / Carbon instance

您必须使用其中之一:UNIX 时间戳、日期字符串 (Ymd)、日期时间字符串、DateTime / Carbon 实例

https://laravel.com/docs/4.2/eloquent#accessors-and-mutators

https://laravel.com/docs/4.2/eloquent#accessors-and-mutators

回答by Senthil

Even though its a year old issue and I'm placing my input for anyone still struggling even after setting the mutator.

即使这是一个已有一年历史的问题,而且即使在设置了 mutator 之后,我仍然为任何仍在苦苦挣扎的人提供我的意见。

If the html input date element passes the date in atom format(1975-12-25T14:15:16-05:00) then the date mutator won't help. You need to apply the following fix in Illuminate\Database\Eloquent\Model class at line#2848 to get it work (in laravel#5).

如果 html 输入日期元素以原子格式 (1975-12-25T14:15:16-05:00) 传递日期,那么日期修改器将无济于事。您需要在第 2848 行的 Illuminate\Database\Eloquent\Model 类中应用以下修复程序才能使其工作(在 laravel#5 中)。

$value = Carbon::createFromFormat($format, (new DateTime($value))->format('Y-m-d H:i:s'));