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?
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:
- https://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-in-laravel-5
- https://tutsforweb.com/creating-helpers-laravel/ (both the composer solution and the service provider one didn't work)
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');
Please or to participate in this conversation.