18

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

a file called DataHelper.php and store it at /app/Helpers/DateHelper.php - and it looks like this

<?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

to be able to called it in my blade view like

DateHelper::dateFormat1($user->created_at)

I'm not sure what to do next.

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

Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604

2 Answers2

17
  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. Run composer dump-autoload

That should do it. :)

code-8
  • 54,650
  • 106
  • 352
  • 604
  • I'm seeing this everywhere. Why would you use a 3rd party tool to include a file that's already within your app? – dKen Nov 18 '15 at 09:35
  • @dKen, composer.json is not a third party tool and it has more use than just fetching third party packages in the 'require' section. It also "autoloads" your php functions, making them available in the runtime of your app. – Timothy Jan 05 '16 at 06:22
  • @TechyTimo `composer` is absolutely third-party. Laravel didn't write it, neither did I, hence third party. It's also [not required for Laravel to work](http://stackoverflow.com/q/15940140/366529), so that's pretty much a great definition of a third party plugin. I also didn't say it didn't have any use, my point was that if it's easy to include the file within the framework, passing it outside of the framework doesn't make a huge amount of sense. [Seems some people are coming around to that point of view](http://stackoverflow.com/a/33776272/366529). – dKen Jan 07 '16 at 13:21
  • 3
    @ihue off topic, but if you copy someone's idea/post, you should at least mention the original URL where you found it... It was originally Jeffrey Way's suggestion I guess. https://laracasts.com/discuss/channels/general-discussion/best-practices-for-custom-helpers-on-laravel-5?page=1 – Sas Sam Apr 24 '16 at 16:16
  • this is not the correct way to do this. please see: http://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-on-laravel-5#32772686 – heisian Apr 27 '16 at 18:45
13

Since your Helper method is static you could add your helper class to config/app alias just like a Facade, like so:

'aliases' => [
    //'dateHelper'=> 'App\Helpers\DateHelper', //for Laravel 5.0
    'dateHelper'=> App\Helpers\DateHelper::class, //for Laravel 5.1
] 

Then later in your view:

{{dateHelper::dateFormat1($user->created_at)}}

However, if you are also looking for a way to do this without a helper class. You may consider using Mutators and Appends in your model:

class User extends Model{
   protected $fillable = [
     'date'
   ];

   protected $appends = [
     'date_format_two'
   ];


   public function getDateAttribute($value){
        $dt = new DateTime($value);
        return $dt->format("m/d/y"); // 10/27/2014
   }

    //date
    public function getDateFormatTwoAttribute($value){
        $dt = new DateTime($value);
        return $this->attributes['date_format_two'] = $dt->format("m, d ,y"); // 10,27,2014
    }
} 

Later you can do

$user = User::find(1);

{{$user->date}}; 
{{$user->date_format_two}}; 
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
  • Thanks for your suggestion :) – code-8 Jun 12 '15 at 14:11
  • I keep getting `App\DateHelper` Not found, and I included it on the top already. Do you know why? Do I have to run `composer update` or something like that to make my alias usable ? – code-8 Jun 12 '15 at 14:22
  • no need for composer update if are using laravel 5, else if Laravel 4, do `composer dump-autoload` Also ensure your namespace is correct. since its located here /app/Helpers/DateHelper.php, the correct namespace is `App\Helpers\DateHelper` – Emeka Mbah Jun 12 '15 at 14:26
  • Work perfectly. The reward is yours - bro. – code-8 Jun 12 '15 at 14:39
  • 3
    I think this is more clear answer than which is accepted – Sanoob Jul 22 '15 at 12:46
  • @Digitlimit thank you so much.i googled for this but your answer cleared my doubt. – scott Jul 29 '15 at 08:59
  • @Digitlimit if we create our own helper class using static method then it is secured or what – scott Jul 29 '15 at 09:02
  • @john I use static method because its easier to use. you can call any method in the class by doing like `dateHelper::anymethod()` – Emeka Mbah Jul 29 '15 at 09:19
  • @Digitlimit.ok .thank you. if we create our own static class then its secured or what .i mean security issues because in normal many are avoiding static methods so asked. – scott Jul 29 '15 at 09:38
  • @Digitlimit.also thank you for posting your answer .its very clear so that i have created helper class very easily – scott Jul 29 '15 at 09:40