Categories
Laravel

How to add custom class in Laravel 5

In this tutorial, I will show you how to add a custom PHP class in Laravel 5.x application. For example, Lets call our class Common.php. Create a directory called Mylibs in app directory along with your class, Ex: Common.php. Make sure to add the correct namespace to the class as for your directory structure, in this example it is App\Mylibs as our common.php file located at App\Mylibs\Common.php

How to add custom class in Laravel 5

Example Custom class in Laravel 5

Here is my final class :

Now, you can access this class using the namespace within your classes just like other Laravel classes.
use App\Mylibs\Common

That is it.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

8 replies on “How to add custom class in Laravel 5”

Thanks for the tutorial! You have forgotten your “;” for the return statement in the common class 🙂

;

I’m not sure but I had to call the function different
Instead of this:
$this->commongetName()

Do this:

$this->common->getName()

Leave a Reply

Your email address will not be published. Required fields are marked *