简体   繁体   English

Laravel:无法在刀片中使用 Carbon diffForHumans

[英]Laravel: Unable to use Carbon diffForHumans in blades

调用字符串上的成员函数 diffForHumans() .. 我实际上在做的是在刀片中获取的日期上调用 diffForHumans。

<span>{{$data['date']->diffForHumans()}}</span>
{{ \Carbon\Carbon::parse($data['date'])->diffForHumans() }}

As already mentioned your $data['date'] probably isn't an instance of Carbon already and therefore needs to be parsed.如前所述,您的 $data['date'] 可能已经不是 Carbon 的实例,因此需要进行解析。 The above will resolve carbon, parse it and get the diff.以上将解析碳,解析它并获得差异。

<span>{{ \\Carbon\\Carbon::parse($data->created_at)->diffForHumans() }}</span>
the $data->iffForHumans(); $data->iffForHumans(); method only works in .PHP file if you add use Carbon\\Carbon;如果添加use Carbon\\Carbon;方法仅适用于 .PHP 文件use Carbon\\Carbon;
BUT in .blade you must write full reference of carbon and parse if to string但是在 .blade 中,您必须编写完整的碳参考并解析是否为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM