Creating Helpers in Laravel 5

Laravel has many helpers in its arsenal, but sometimes you may need to create your own Helpers for any number of reasons. The last Laravel Helper I coded was a helper that can be called globally to send an sms message to an user.

There are sevral ways you can use helpers and you are by no means limited to what you can do, for example, a common use that I have is to create a helper to format dates and do stuff with them, which i will use this as an example in this post.

So lets begin

First off we will need to create a folder in the app directory called Helpers to hold the Helper Files. You should now have App/Helpers.

Now we need to create the helper file itself by creating a File called DatesHelper.php in the App/Helpers directory.

In here we need to add the code we will be using

<?php
namespace App\Helpers;

class DatesHelper {

    // Our code will be here...

}

Now that we have our helper in place, we now need to make it visible to use by Laravel.

Lets create a Service Provider using artisan and generate a provider file,

$php artisan make:provider DatesServiceProvider 
Provider created successfully.

This should now have create a DatesServiceProvider in App\Providers folder.

In the App\Providers\DatesServiceProvider we need to add the folowing

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

This will register our dependancies.

Now we need to register it like a package and add it to the config/app.phpunder Application Service Providers and add a Class Alias

'providers' => [
    App\Providers\DatesServiceProvider::class,
],

'aliases' => [
    'DateHelper' => App\Helpers\DatesHelper::class,
],

Ok so now the fun begins where we can start creating the actual helpers and using them.

So for this example lets import Carbon or add use Carbon\Carbon; to your DatesHelper.php file.

And for our first method we will get the date and transform it to a date string. (its not really useful but its an example)

/**
 * @param int $date Carbon
 *
 * @return string
 */
public static function formatted_date_string($date) {
    $readable_date = Carbon::parse($date)->toFormattedDateString();
    return $readable_date;
}

Next, as a test we can create a route method and pass a date through like so

use App\Helpers\DatesHelper;

Route::get('/date-to-string', function () {
    return DatesHelper::formatted_date_string(Carbon\Carbon::today());
});

// result (date) "2018-09-27"

Obviously you can do whatever you want in these helpers and this is just a basic example. For a more common use like a recenlt project using sms the code below takes the $user and $message variable and uses it to send an sms dynamically.

/**
 * Sends a text message via nexmo
 * @param array $user User
 * @param string $message
 *
 * @return array
 */
public static function send($user,$message) {

    if($user->details->telephone != null){

        $data = Nexmo::message()->send([
            'to'   => $user->details->telephone,
            'from' => 'My App',
            'text' => $message
        ]);

        return $data;

    } else {

        $data = 'No Number Provided';
        return $data;
    }
}


Conclusion

Helpers are a brilliant way of tidying up your code when refactoring. And whats best is its really easy to do.