问题描述
默认的 created_at 日期继续打印为 MySQL 格式:2015-06-12 09:01:26.我想像 12/2/2017 一样以我自己的方式打印它,以及未来的其他格式.
the default created_at date keep printing out as an MySQL format : 2015-06-12 09:01:26. I wanted to print it as my own way like 12/2/2017, and other formats in the future.
一个名为 DataHelper.php 的文件并将其存储在 /app/Helpers/DateHelper.php - 它看起来像这样
a file called DataHelper.php and store it at /app/Helpers/DateHelper.php - and it looks like this
<?php namespace AppHelpers; class DateHelper { public static function dateFormat1($date) { if ($date) { $dt = new DateTime($date); return $dt->format("m/d/y"); // 10/27/2014 } } }
我想要
能够在我的刀片视图中调用它
I want
to be able to called it in my blade view like
DateHelper::dateFormat1($user->created_at)
我不知道接下来要做什么.
I'm not sure what to do next.
在 php Laravel 5 中创建自定义辅助函数的最佳实践是什么?
推荐答案
- 在您的 app/Http 目录中,创建一个 helpers.php 文件并添加您的函数.
- 在 composer.json 中,在 autoload 块中,添加 "files": ["app/Http/helpers.php"].
- 运行composer dump-autoload
- Within your app/Http directory, create a helpers.php file and add your functions.
- Within composer.json, in the autoload block, add "files": ["app/Http/helpers.php"].
- Run composer dump-autoload
应该可以.:)