Create custom helper functions / Classes in Laravel 8/9 and use them.

Chimeremze Prevail Ejimadu
3 min readOct 17, 2022

--

Photo by Peter Masełkowski on Unsplash

Helper functions are functions (usually generalized) that we can call in any part of our application. Laravel comes with a number of really useful and lovely built-in helper functions, and so many developers will always appreciate the dd(). A few of my favorites include url(), route(), asset(), env(), app(), request(). You can find the full list of these here https://laravel.com/docs/9.x/helpers

But as developers, sometimes we need to write some functions that will help eliminate repetitive code and add speed to our development. So I will show you how to create custom helper classes and use them in Laravel8/9 projects. I will do this with a real life case study.

What do we want to do?

In this application, I want to Log all activities as notifications in a table. When they log in, when they comment, when they visit a page, when they like, post, follow, and any action we might think of.

We might want to send emails to different users for different actions performed, there are many ways to do this, one of which I showed you in this tutorial HERE.

Step 1: Create a helper.php file in the laravel app folder.

The helpers.php files often are usually found in the following places:

app/Http/helpers.php
app/Helpers/helpers.php
app/helpers.php

For this lesson, we’ll use app/Helpers/helpers.php.

Step 2: Write your function in app/Helpers/helpers.php.

<?phpnamespace App\Helpers;use App\Models\Notification;class NotifyClass
{
private $data = null;
public function __construct($data)
{
$this->data = $data;
}
public function make(): bool
{
$notify = Notification::create([
'user_id' => $this->data['user_id'],
'type' => $this->data['type'],
'created_at' => now()
]);
if ($notify)
return true;
else
return false;
}
}

The next thing to do is to learn how to make use of this helper class.

In your controllers, you just have to add the namespace and call it.

use App\Helpers\NotifyClass;
//after following a user,
$data = [
'user' => Auth::user()->id,
'type' => 'follow'
]
(new NotifyClass($data))->make();//after liking a post,
$data = [
'user' => Auth::user()->id,
'type' => 'like'
]
(new NotifyClass($data))->make();//after ordering a product,
$data = [
'user' => Auth::user()->id,
'type' => 'order'
]
(new NotifyClass($data))->make();(new NotifyClass($data))->make();ifTrueDoSomething
elseDoSomethingElse

There are many things you can do.

Now, we can call the notify helper class and log a user’s action into the db, we can can add an “action_id” or “notifiable_id” that we can use to save the id receiving the user’s action. For example, a user orders a product, action_id will be the id of the product, and type will be ‘order’

use App\Helpers\NotifyClass;
//after ordering a product,
$data = [
'user' => Auth::user()->id,
'type' => 'order'
'action_id' => 784 //product id
]
(new NotifyClass($data))->make();

It’s all about what you want to do.

There are several ways to use helper classes in laravel. You can make static functions for some basic use and much like anything you want to use multiple times in a generic manner.

There are methods that require you to autoload these functions and all, but here we made it KISS simple. Try it out and use it in your next project.

Stay tuned!!! I will be back with some more cool Laravel tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some clap 👏. And if you have any questions feel free to comment.

Thank you 🙏

Thanks a lot for reading till end 🙏 You can contact me in case if you need any assistance:
Email: prevailexcellent@gmail.com
Github: https://github.com/PrevailExcel
LinkedIn: https://www.linkedin.com/in/chimeremeze-prevail-ejimadu-3a3535219
Twitter: https://twitter.com/EjimaduPrevail

--

--

Chimeremze Prevail Ejimadu

Laravel Developer + Writer + Entrepreneur + Open source contributor + Founder + Open for projects & collaborations. Hit FOLLOW ⤵