Search code examples
phplaravellaravel-5laravel-5.3

Class not found when called in custom class in laravel


I have a file named

Helper.php and I have put it in composer.

"autoload": {
        "files": [
            "app/Http/Helper.php",
            "app/Notification.php"
        ]
    },

You can see that I have also put a model that is Notification model. but when I call this Notification in Helper.php it says class Notification not found..

function notify()
{
    $notify = new App\Notifcation;
    $notify = new Notifcation; //Also tried this

}

Solution

  • First of all you don't need to add it in composer.

    Secondly, check what you have written twice thrice because there can be typos that will stop your program from being executed

    remove "app/Notification.php" from composer.json and dump-autoload it. Then use like this.

    function notify()
    {
        $notify = new App\Notification;
    }
    

    If you add this Notification Model in composer then it will always be autoloaded even if it is not used thus putting unnecessary pressure on your project.

    Hope this helps