找不到文字月份,追踪数据Carbon-laravel [英] A textual month could not be found ,Trailing data Carbon - laravel
问题描述
我确实尝试了不同的方法,但是我没有获得正确的时间格式.
$news->created_at = Carbon::parse($news->created_at)->format('M, d, Y');
$news->created_at = date('d M Y',strtotime($news->created_at))
$news->created_at = date('d M Y',$news->created_at)
$news->created_at = Carbon::createFromFormat("d M Y",strtotime($news->created_at));
$news->created_at = $news->created_at->format('M, d, Y');
错误是
找到意外数据
找不到分隔符号
InvalidArgumentException
Carbon.php: 910
dd($ news-> created_at);
Carbon @1550035143 {#361 ▼
date: 2019-02-13 05:19:03.0 UTC (+00:00)
}
在您的$news->created_at
字段中已经有一个Carbon实例,因为雄辩的模型考虑了 format
方法
$news->created_at->format('d M Y');
但是,当您尝试在模型实例上将字符串重新分配为created_at
的值时,它与Laravel的内部增变器发生冲突,后者试图将分配给日期字段的任何值从Carbon实例转换为字符串. /p>
您可以在News
模型中设置public $timestamps = false;
,然后在处理模型时间戳时在整个应用程序中使用字符串,但这似乎是一种破解,而不是解决方案,因为您将放弃所有的好处碳优惠.
您还可以通过在序列化时处理时间戳来完成此操作,如下所示:
return collect($news->makeHidden(['created_at']))->merge([
'created_at' => $news->created_at->format('d M Y')
]);
上面的代码将隐藏序列化过程中传递给makeHidden
的列.然后,您可以将隐藏列的格式化值合并到响应中.
I did try different ways, but I have not get the proper time format.
$news->created_at = Carbon::parse($news->created_at)->format('M, d, Y');
$news->created_at = date('d M Y',strtotime($news->created_at))
$news->created_at = date('d M Y',$news->created_at)
$news->created_at = Carbon::createFromFormat("d M Y",strtotime($news->created_at));
$news->created_at = $news->created_at->format('M, d, Y');
And the errors are,
Unexpected data found
The separation symbol could not be found
InvalidArgumentException
Carbon.php: 910
dd($news->created_at);
Carbon @1550035143 {#361 ▼
date: 2019-02-13 05:19:03.0 UTC (+00:00)
}
You already have a Carbon instance in your $news->created_at
field, because Eloquent models consider the created_at
and updated_at
columns as timestamps by default and automatically convert them to Carbon
instances. So you just need to use the format
method from Carbon:
$news->created_at->format('d M Y');
However, when you try to reassign a string as the value of created_at
on the model instance, it conflicts with Laravel's internal mutator that tries to convert any value assigned to a date field from a Carbon instance into a string.
You could set public $timestamps = false;
in your News
model and then use strings throughout your app when handling model timestamps, but that seems like a hack more than a solution, because you'd be giving up on all the benefits that Carbon offers.
You also could also do this by handling timestamps at serialization time, something like this:
return collect($news->makeHidden(['created_at']))->merge([
'created_at' => $news->created_at->format('d M Y')
]);
The above code will hide the columns passed to makeHidden
from the serialization process. Then you could merge the formatted values for the hidden columns into your response.
这篇关于找不到文字月份,追踪数据Carbon-laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!