4

I am trying to load my app.js file inside my welcome.blade.php but I get the following error:

Call to undefined function asset()

This is the instruction I used <script type="text/javascript" src="{{ asset('public/js/app.js') }}"></script>

What can be the problem? I also tried to run the same line without asset() and it does not work. My .js file is in the location I specified.

Thank you so much!

3
  • Have you been modifying which files get loaded by the composer autoloader in any way? Commented Jun 16, 2018 at 10:37
  • I do not think so. Actually I tried to load it with blade: { { Html::script('public/js/app.js') }} and it seems like it does not recognize the Blade syntax either. Commented Jun 16, 2018 at 11:30
  • Can you try using asset('public/app.js') from php artisan tinker and say if it works? Commented Jun 16, 2018 at 16:04

3 Answers 3

1

Process:

I am using Laravel 5.5 and had the same problem recently so I did a bit of debugging. Here are the steps:

  1. Since the error was Call to undefined function asset() I checked the helpers.php in the Laravel repo to see if it existed. https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/helpers.php#L127 and it did.

    if (! function_exists('asset')) { /** * Generate an asset path for the application. * * @param string $path * @param bool $secure * @return string */ function asset($path, $secure = null) { return app('url')->asset($path, $secure); } } 
  2. I looked on my local copy of Laravel and see if that same line existed, it did.

  3. However my local copy was different than what was on the repo. This is how it looked:

    if (! function_exists('asset')) { /** * Generate an asset path for the application. * * @param string $path * @param bool $secure * @return string */ function mixasset($path, $secure = null) { return app('url')->asset($path, $secure); } } 
  4. At some point, Laravel Mix seems to have changed asset() to mixasset().


Result:

Try using mixasset() as opposed to asset(). Otherwise delete your vendors folder and rebuild it by running composer install. This should get you the latest copy of your Laravel version with the correct asset() helper method.

Sign up to request clarification or add additional context in comments.

Comments

0

The asset() is already point to your public folder so, don't specify the public in your asset() Change your asset('public/js/app.js') to asset('js/app.js')

2 Comments

It does not change anything :(
The asset function is undefined, changing its params won't do much.
0

My Err: Call to undefined function set_active()...., when I move some functions in Laravel 5.2 to Laravel 6.x ... My solution: 1. Added below in composer.json "autoload": { "classmap": [ "database" ], "psr-4": { "App\": "app/" }, "files": [ "app/helpers.php" . => New added ] } 2.run composer install it works for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.