Top Questions Ask a Question Answer a Question Blogs Write a Blog
How to make Helper own custom helper function in laravel ?
1 answer, 0 shares, 303 views

9 months ago

laravel helper function


  1. Make file helpers.php in app directory (or anywhere)
  2. Add files line in composer.json

"autoload": {

"files": [

          "app/helpers.php"

],

"classmap": [

           "database/seeds",

           "database/factories"

],

"psr-4": {

          "App\\": "app/"

  }

},

Once you add a new path to the files array, you need to dump the autoloader:

composer dump-autoload

Now write function to the helpers.php, (here is simple custom function example which return user avatar if user is logged in else return the default image)

<?php
 

function user_avatar(){

    if(auth()->check()){

        return auth()->user()->avatar;

    }else{

        return url('/img/user.png');

   } 

}

 

and you can access this function by calling 

<?php  user_avatar() ?>