Tuesday, October 30, 2018

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





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.









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




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

}







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?


Answer




  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. :)


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...