Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ByteShaman's avatar

Problems with a custom helper function

I'm having some problems using a function defined in a custom Helper file :s

The file is: app\Helpers\Helper.php

<?php

namespace App\Helpers;

class Helper
{
  public static function logg($var)
  {
    echo "<script>console.log(`" . print_r($var, true) . "`);</script>";
  }
}

Inside aliases array in config\app.php: 'Helper' => App\Helpers\Helper::class, I gave composer dump-autoload php artisan config:clear and php artisan config:cache commands in the root folder of the project.

In the controller I need to use the function I have: use Helper; and when I call logg it doesn't work (Undefined function 'App\Http\Controllers\Auth\logg' so it searches for the function in the wrong place: but I have set the alias for it?). If I use use App\Helpers\Helper; and then Helper::logg() it does work (however, logg() without prefix still doesn't work), can someone help?

Laravel version: 5.5 PS: I know there is Log function in Laravel, I actually need to use other helper functions that would take too much space so I just wrote a simple function.

Here are two resources I tried before posting in the forum:

  1. https://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-in-laravel-5
  2. https://tutsforweb.com/creating-helpers-laravel/ (both the composer solution and the service provider one didn't work)
0 likes
18 replies
PaulMaxOS's avatar

console.log() is something that you would do on the client side. So what is it that you're trying to achieve when echo it from within a controller which is not on the client side of things?

ByteShaman's avatar

I want to display information on the status of variables and text in the JavaScript console since I can't install XDebug and Debugbar's logs can't display complex information like objects or HTTP requests...

newbie360's avatar

since <script>......</script> is work for front-end, make a component instead helper ?

newbie360's avatar

@n00bcod3r

however, logg() without prefix still doesn't work

so try to create a Helper function

project-folder/app/Helpers/Helper.php

<?php

if (! function_exists('hello')) {
    /**
     * return "hello"
     *
     * @return string
     */
    function hello(): string
    {
        return 'hello';
    }
}

if (! function_exists('world')) {
    /**
     * return "world"
     *
     * @return string
     */
    function world(): string
    {
        return 'world';
    }
}

// here is no close tag ?>

project-folder/composer.json , add files the autoload

    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": [
            "app/Helpers/Helper.php"
        ]
    },

run composer dump-autoload

project-folder/vendor/composer/autoload_files.php , check the last line should look like this

    '----------long string------' => $vendorDir . '/barryvdh/laravel-debugbar/src/helpers.php',
    '----------long string------' => $baseDir . '/app/Helpers/Helper.php',

done

use it anywhere

$str = hello() . ' ' . world();
{{ hello() }}

also you can use Snippets in your editor, for example VS code

default can use log press tab

https://code.visualstudio.com/docs/editor/userdefinedsnippets

ByteShaman's avatar

Hello, I already did all of this, but even when I call the function using the prefix, no message is shown in the console...

EDIT: I didn't actually try to use return instead of echo, I will now! Nope, it still doesn't print in the console.

newbie360's avatar

can you show the html source code from browser

jeffreyvanrossum's avatar

Just to be sure, is your use Helper; statement below the namespace declaration?

ByteShaman's avatar

Yes, it's under namespace App\Http\Controllers\Auth; I put it after others use statements

jeffreyvanrossum's avatar

Hmm, when I follow the steps as you've described, I don't run into issues.

ByteShaman's avatar

I modified my files in the following way

Helper.php is just made by the function

<?php

function logg($var)
{
  return "<script>console.log(`" . print_r($var, true) . "`);</script>";
}

Then I have in my composer.json:

 "autoload": {
        "files": [
          "app/Helpers/Helper.php"
        ],

But if I call the function logg nothing is printed in the console.

ByteShaman's avatar

I tried both echo and return, in this version I use return because I didn't remember to set it back to echo: it doesn't log to console anyway

newbie360's avatar

because laravel {{ }} escaped the < >

try

{!! logg('test') !!}
ByteShaman's avatar

The logg function is called from inside a controller method (first instruction of the login() method), not from a blade page tho

newbie360's avatar

oops, in modern browser, if you return $var; it will show in json format

hmm i don't know how to do that ;(

newbie360's avatar
Level 24

what about just return

if (! function_exists('logg')) {

    function logg($var): string
    {
        return "<script>console.log(`" . print_r($var, true) . "`);</script>";
    }
}

and use it in controller

return logg('test');
ByteShaman's avatar

This works, I just used echo in both functions because I didn't want the script to terminate ^^

Please or to participate in this conversation.