Laravel碳数据缺失

Laravel碳数据缺失

本文介绍了Laravel碳数据缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  protected $ dates = [
'start',
'end',
'created_at',
'updated_at'
];

我使用日期时间选择器来插入开始和结束日期,格式如下:

  2016-01-23 22:00 

没有秒。当我这样做时,我得到这个错误:

  Carbon.php中的InvalidArgumentException行425:
数据缺失在Model.php 3015
p $ p>

如果包含秒数,秒对我来说并不重要,我不希望将它们包含在我的日期时间选择器字段中。任何方式,所以我仍然可以使用这些字段作为日期字段?

解决方案

tl; dr



您的日期字符串和您的日期格式不同,您必须更改格式字符串或修改日期字符串以匹配。





问题



当Carbon的 createFromFormat 函数接收与传递的格式字符串不匹配的日期字符串。更确切地说,这来自函数,因为Carbon只是调用了:

public static function createFromFormat ($格式,$时间,$ tz = null)
{
if($ tz!== null){
$ dt = parent :: createFromFormat($ format,$ time,static: :safeCreateDateTimeZone($ TZ));
} else {
$ dt = parent :: createFromFormat($ format,$ time); //发生错误的地方


if($ dt instanceof DateTime){
return static :: instance($ dt);
}

$ errors = static :: getLastErrors();
抛出新的InvalidArgumentException(implode(PHP_EOL,$ errors ['errors'])); //抛出异常的地方



$ b $ h $没有足够的数据


$ b

  Carbon :: createFromFormat('Ymd H:i:s','2017-01-04 00:52'); 

碳会抛出:



数据太多 h3>

如果你的日期字符串比这种情况下的格式字符串长一些:

  Carbon :: createFromFormat('Ymd H:i','2017-01-02 00:27:00'); 

碳会抛出:



h2>

根据默认的日期格式是:'Ymd H:i:s'。日期处理发生在。在最后的情况下,被调用,那是自定义格式的来源。默认格式是在数据库的<$ c中定义的


$ b


$ b 您有以确保日期字符串与格式字符串匹配。



更改格式字符串



您可以覆盖默认的格式字符串是这样的:

class Event extends Model {
protected $ dateFormat = 'Ymd H:我';



$ b $ p
$ b

这个方法有两个问题:


  • 这将应用于模型的 $日期数组中定义的每个字段。
  • 您必须以这种格式将数据存储在数据库中。



编辑和格式化日期字符串



我推荐的解决方案是日期格式应该保持默认'Ymd H:i:s',你应该完成缺少的部分日期,如下所示:
$ b

  public function store(Request $ request){
$ requestData = $ request-> all();
$ requestData ['start_time']。=':00';
$ requestData ['end_time']。=':00';
$ event = new Event($ requestData);
$ event-> save();

$ / code>

当你想使用日期格式时:

  public function show(Request request,$ eventId){
$ event = Event :: findOrFail ($事件ID);
$ startTime = $ event-> start_time->格式('Y-m-d H:i');
$ endTime = $ event-> end_time-> format('Y-m-d H:i');

当然,这些字段应该被改变为日期:

class Event extends Model {
protected $ dates = [$ b $'start_time',
'end_time',
'created_at',
'updated_at',
'deleted_at',
];
}


In my model I have the following:

protected $dates = [
    'start',
    'end',
    'created_at',
    'updated_at'
];

I am using a datetime picker to insert the start and end dates, in this format:

2016-01-23 22:00

Without the seconds. When I do it like this, I get this error:

InvalidArgumentException in Carbon.php line 425:
Data missing
at Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 22:00') in Model.php line 3015

If I do include the seconds, it works. The seconds are not important to me, and I do not want to include them in my datetime picker fields. Any way around this so I can still use those fields as date fields?

tl;dr

Your date string and your date format is different, you have to change the format string or modify the date string so they match.

Explanation

The Problem

This error arises when Carbon's createFromFormat function receieves a date string that doesn't match the passed format string. More precisely this comes from the DateTime::createFromFormat function, because Carbon just calls that:

public static function createFromFormat($format, $time, $tz = null)
{
    if ($tz !== null) {
        $dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
    } else {
        $dt = parent::createFromFormat($format, $time); // Where the error happens.
    }

    if ($dt instanceof DateTime) {
        return static::instance($dt);
    }

    $errors = static::getLastErrors();
    throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors'])); // Where the exception was thrown.
}

Not enough data

If your date string is "shorter" than the format string like in this case:

Carbon::createFromFormat('Y-m-d H:i:s', '2017-01-04 00:52');

Carbon will throw:

Too much data

If your date string is "longer" than the format string like in this case:

 Carbon::createFromFormat('Y-m-d H:i', '2017-01-02 00:27:00');

Carbon will throw:

Under the hood

According to the documentation on mutators the default date format is: 'Y-m-d H:i:s'. The date processing happens in the Model's asDateTime function. In the last condition the getDateFormat function is called, thats where the custom format comes from. The default format is defined in the Database's Grammar class.

Solution

You have to make sure that the date string matches the format string.

Change the format string

You can override the default format string like this:

class Event extends Model {
    protected $dateFormat = 'Y-m-d H:i';
}

There is two problem with this approach:

  • This will apply to every field defined in the model's $dates array.
  • You have to store the data in this format in the database.

Edit and format the date strings

My recommended solution is that the date format should stay the default 'Y-m-d H:i:s' and you should complete the missing parts of the date, like this:

public function store(Request $request) {
    $requestData = $request->all();
    $requestData['start_time'] .= ':00';
    $requestData['end_time'] .= ':00';
    $event = new Event($requestData);
    $event->save();
}

And when you want to use the date you should format it:

public function show(Request request, $eventId) {
    $event = Event::findOrFail($eventId);
    $startTime = $event->start_time->format('Y-m-d H:i');
    $endTime = $event->end_time->format('Y-m-d H:i');
}

Of course the fields should be mutated to dates:

class Event extends Model {
    protected $dates = [
        'start_time',
        'end_time',
        'created_at',
        'updated_at',
        'deleted_at',
    ];
}

这篇关于Laravel碳数据缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:29