php - laravel format() 函数仅适用于某些 Blade

标签 php laravel

我对 laravel 的 format() 函数感到非常困惑。在某些 Blade 中 format() 工作正常。但是在 user.blade 中只有当我添加以下内容时才有效功能

public function getDates()
{
    return array('created_at', 'updated_at', 'deleted_at', 'last_login_at');
}

用户.blade

<td>{{ $user->created_at->format('d/m/Y')}}</td>

并且在我的事件 Blade 中甚至添加了 getDates() 函数也无法工作。这背后的原因是什么?提前致谢。

Error =Call to a member function format() on string (View: C:\inetpub\wwwroot\odata\resources\views\activity\list.blade.php)

最佳答案

它给你错误,因为format()是Carbon的函数。

在您的 Blade 中使用以下代码。

 {{\Carbon\Carbon::parse($user->created_at)->format('d/m/Y')}}

要了解有关 Carbon 的更多信息,请访问 http://carbon.nesbot.com/docs/ .

关于php - laravel format() 函数仅适用于某些 Blade ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41162257/

相关文章:

php - Laravel 唯一验证在更新时失败

php - 使用 mailgun 发送电子邮件时尝试获取非对象的属性

php - Cakephp 3.0 中的自定义查询?

php - LARAVEL - 按钮更改数据库中的 bool 值

php - Laravel 获取关系项的集合

php - 覆盖 FormRequest 的 failedValidation 方法时,Laravel 6 自定义验证错误 'class does not exist'

php - 无法登录 MySQL/mariaDB - [错误] 无法加载资源 : the server responded with a status of 500 (Internal Server Error) (get_data. php)

php - Ajax 调用文件

php - 应用从数据库获取DATETIME-时区问题

laravel - Laravel 中的自定义主键不起作用