I have

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.


I created

一个名为DataHelper.php的文件,并将其存储在/app/Helpers/DateHelper.php-它看起来像这样

<?php

namespace App\Helpers;

class DateHelper {

    public static function dateFormat1($date) {
        if ($date) {
            $dt = new DateTime($date);

        return $dt->format("m/d/y"); // 10/27/2014
      }
   }
}

I want

能在我的刀锋视野中称之为

DateHelper::dateFormat1($user->created_at)

我不知道下一步该怎么办.

What is the best practice to create a custom helper function in php Laravel 5?

推荐答案

  1. Within your app/Http directory, create a helpers.php file and add your functions.
  2. Within composer.json, in the autoload block, add "files": ["app/Http/helpers.php"].
  3. composer dump-autoload

That should do it. :)

Laravel相关问答推荐

try 编写一个函数,如果产品存在,则无法删除特定类别

为什么只删除了最后一个Like,而没有删除选中的?

Vue 组件中的图像不适用于 Vite + Laravel

验证判断请求的值是否存在于另一个表 Laravel 9 中

在 Laravel 中设置多个 Vue 组件的问题

如何获得每种类型的总和

Laravel Homestead vagrant up 超时

所有 POST 请求上的 Laravel 4 CSRF

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

你如何找到 Laravel 外观背后的底层类?

Laravel Blade 没有额外的空格?

如何卸载 Laravel?

如何在 Laravel 5.5 中为选定的请求类设置自定义响应

Http请求多浏览器的烦恼

控制器外部的 Laravel 访问请求对象

Elastic search全文与mysql全文?

在 Laravel 的数据透视表中添加 id 列有什么好处?

Laravel 5 - 判断日期是否是今天

在哪里放置 Blade::extend

如何在不要求用户登录 Laravel 的情况下验证Electron邮件