javascript - Call to undefined function asset() in Laravel project - Stack Overflow

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

I am trying to load my app.js file inside my wele.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!

I am trying to load my app.js file inside my wele.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!

Share Improve this question asked Jun 16, 2018 at 7:37 JoeJoe 2452 gold badges5 silver badges13 bronze badges 3
  • Have you been modifying which files get loaded by the poser autoloader in any way? – J. Arbet 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. – Joe Commented Jun 16, 2018 at 11:30
  • Can you try using asset('public/app.js') from php artisan tinker and say if it works? – J. Arbet Commented Jun 16, 2018 at 16:04
Add a ment  | 

3 Answers 3

Reset to default 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./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 poser install. This should get you the latest copy of your Laravel version with the correct asset() helper method.

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')

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 poser.json "autoload": { "classmap": [ "database" ], "psr-4": { "App\": "app/" }, "files": [ "app/helpers.php" . => New added ] } 2.run poser install it works for me.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281226a4414521.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信