laravel 8.x 时间字段 调用 diffForHumans() 报 Call to a member function diffForHumans() on string 错误
示例代码:
xxx.blade.php 页面调用
{{ $product->end_at->diffForHumans(now()) }}
解决方法1:
{{ \Carbon\Carbon::parse($product->end_at)->diffForHumans(now()) }}
解决方法2:
把Model 文件中定义的:
protected $datas = [
'end_at'
];
修改成
protected $casts = [
'end_at' => 'date',
];
就不报错了!