gpt4 book ai didi

php - Laravel 日期不允许我使用 diffForHumans

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:56:32 25 4
gpt4 key购买 nike

在我的表中设置一个时间戳字段,以便在字段中创建和更新。

然后在我的模型中我这样做:

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

但是调用日期时:

$p->created_at->diffForHumans()

我明白了

Call to a member function diffForHumans() on string

我很确定这应该有效。我之前在不同型号等上多次使用相同的方法,但这行不通。

最佳答案

默认情况下,Eloquent 会将 created_at 和 updated_at 列转换为 Carbon 的实例,它提供了各种有用的方法,并扩展了原生的 PHP DateTime 类。

阅读:https://laravel.com/docs/5.1/eloquent-mutators#date-mutators

只需检查您的 composer.json 文件中的 nesbot/carbon 包。如果你没有,你可以通过输入

安装它
composer require nesbot/carbon

替代方案是,

您可以使用 Carbon::parse() 动态创建对象。

Carbon::parse($p->created_at)->diffForHumans();

关于php - Laravel 日期不允许我使用 diffForHumans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34278455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com