0

I want to make some changes to dates, for example, I want to explode it do some operation and implode it again, and I want to use it all around my app so here is my code :

$divided_date = explode('/', $request->point_date);
$converted_date = Verta::getGregorian($divided_date[0], $divided_date[1], $divided_date[2]); // [2015,12,25]
$begindate = implode('/', $converted_date);

I want to make a function called DateConvertor() for example, and anywhere that I want to convert the date I use something like below.

$request->somedate()->DateConvertor();

or for example

Dateconvertor($request->someday);

And it returns the converted date so now I don't know to use the static method or no, and I don't know where to define it so I can use it in all of my app not just a single model.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Farshad
  • 1,830
  • 6
  • 38
  • 70

2 Answers2

2

create a function in a php file and add it in composer.json file inside "autoload" attribute, like this:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files": [
        "app/Helpers/CustomHelper.php"
    ]
}

and run composer dump-autoload command, Here - CustomHelper.php is the file, i want to autoload and their function can be used anywhere in your project

CustomHelper.php

<?php

  if (! function_exists('getOTP')) {
    function getOTP()
    {
        return str_pad(rand(0, pow(10, 4) - 1), 4, '0', STR_PAD_LEFT);
    }
  }

 ?>

now you can call getOTP() function anywhere in your project

2

You can create a Helper.php file and in composer.json include that file as

"files": [
        "app/Helpers/Helper.php",   
]

or may add a helper class like

<?php


namespace App\Helpers;


class Helper
{
    public static function foo()
    {
        return 'foo';
    }
}

and config/app.php

'aliases' => [

    ....

    'Helper' => App\Helpers\Helper::class,

]

and then use as Helper::foo();

or add a service provider like

php artisan make:provider HelperServiceProvider 

in register method

public function register()
{
    require_once app_path() . '/Helpers/Custom/Helper.php';
}

In config/app.php

providers = [ 'CustomHelper' => App\Helpers\Custom\Helper::class, ]

and

'aliases' => [
'CustomHelper' => App\Helpers\Custom\User::class,
]

then use as

CustomHelper::foo();
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • i am sorry if its so basic and so silly i want to do something like this syntax for example a user send me a variable called date so $date->convertdate(); and it convert dates for me so can it be the helper again ?? – Farshad Apr 12 '19 at 15:08
  • 1
    @Farshad yes you can use a helper or just use directly in view, as this is not effecting your real data. – Prafulla Kumar Sahu Apr 12 '19 at 15:10
  • thanks for your very complete and awesome answer i will try it and let u know if i got any problem which i think i should not :) – Farshad Apr 12 '19 at 15:13