php Laravel 碳数据丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34968061/
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
Laravel Carbon Data Missing
提问by Hardist
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?
如果我确实包括秒数,它就可以工作。秒对我来说并不重要,我不想将它们包含在我的日期时间选择器字段中。有什么办法可以解决这个问题,所以我仍然可以将这些字段用作日期字段?
回答by totymedli
tl;dr
tl;博士
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:
当 Carbon 的createFromFormat
函数接收到与传递的格式字符串不匹配的日期字符串时,就会出现此错误。更准确地说,这来自于DateTime::createFromFormat
函数,因为 Carbon 只是调用了它:
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:
碳会抛出:
InvalidArgumentException in Carbon.php line 425:
Data missing
Carbon.php 第 425 行中的 InvalidArgumentException:
数据丢失
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:
碳会抛出:
InvalidArgumentException in Carbon.php line 425:
Trailing data
Carbon.php 第 425 行中的 InvalidArgumentException:
尾随数据
Under the hood
引擎盖下
According to the documentation on mutatorsthe 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
functionis called, thats where the custom format comes from. The default format is defined in the Database's Grammar
class.
根据有关 mutators的文档,默认日期格式为:'Y-m-d H:i:s'
. 日期处理发生在模型的asDateTime
函数中。在最后一个条件下,该getDateFormat
函数被调用,这就是自定义格式的来源。默认格式在数据库的Grammar
类中定义。
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.
- 这将适用于模型
$dates
数组中定义的每个字段。 - 您必须以这种格式将数据存储在数据库中。
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:
我推荐的解决方案是日期格式应保持默认值'Y-m-d H:i:s'
,您应该完成日期的缺失部分,如下所示:
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',
];
}
回答by subzeta
You can set the $dateFormat in your model as Christian says, but if you don't want to imply the updated_at and created_at fields into the operation you can use events to "correct" the datetime object before saving it into the database.
您可以像 Christian 所说的那样在模型中设置 $dateFormat,但是如果您不想将 updated_at 和 created_at 字段暗示到操作中,您可以使用事件在将日期时间对象保存到数据库之前“更正”它。
Here you have the official doc about it: https://laravel.com/docs/5.2/eloquent#events
这里有关于它的官方文档:https: //laravel.com/docs/5.2/eloquent#events
回答by Cristhian Carre?o
Models
楷模
This function disabled, the emulations for carbon in Datetimes https://laravel.com/docs/5.0/eloquent#date-mutators
此功能已禁用,Datetimes 中的 carbon 模拟 https://laravel.com/docs/5.0/eloquent#date-mutators
public function getDates()
{
return [];
}
回答by mazedlx
You need to set protected $dateFormat
to 'Y-m-d H:i
' in your model, see https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
您需要在模型中设置 protected $dateFormat
为'Y-m-d H:i
',请参阅https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
回答by davidprogramador01
Make sure you are not omitting the "created_at" or "updated_at" fields in some rows in your database, which are required; if that is the case delete the records where they have those empty fields or enter a value in the valid timestamp format, example '2018-09-01 15:18:53'.
确保您没有省略数据库中某些行中的“created_at”或“updated_at”字段,这是必需的;如果是这种情况,请删除包含这些空字段的记录或以有效的时间戳格式输入值,例如“2018-09-01 15:18:53”。
回答by Inamur Rahman
This is one possibility
这是一种可能
You need to check the column of resultant data. If your column name 'created_at' or 'updated_at' is null, then it will through this error.
您需要检查结果数据的列。如果您的列名 'created_at' 或 'updated_at' 为空,那么它将通过此错误。
How to Solve ?First of all, you need to store the data in those two columns using Laravel carbon. Eg:
怎么解决 ?首先,您需要使用 Laravel carbon 将数据存储在这两列中。例如:
$user = new User();
$user->created_at = Carbon::now()->setTime(0,0)->format('Y-m-d H:i:s');
$user->updated_at = Carbon::now()->setTime(0,0)->format('Y-m-d H:i:s');
$user->save();
That's All, I hope it will work
就是这样,我希望它会起作用
Happy Coding....
快乐编码....
回答by tety94
For me the problem was with SQLServerGrammar located into the vendor (vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\SqlServerGrammar.php).
Default in SQLServerGramma is Y-m-d H:i:s.v
.
对我来说,问题在于位于供应商 (vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\SqlServerGrammar.php) 中的 SQLServerGrammar。SQLServerGramma 中的默认值是Y-m-d H:i:s.v
.
We extended the class end removed .v
.
我们将扩展类结束删除.v
。