user761100
user761100

Reputation: 2317

Laravel 9: Where to place common javascript file?

In my Laravel 9 project that I am working on, I want to put a javascript file containing common functions in app.blade.php. As per How to include External CSS and JS file in Laravel 5.5, I placed the js file in public/js folder and included the following in app.blade.php

 <!-- common js functions added by Ravi  -->
<script src="{{ asset('js/common_js.js') }}" ></script>

However, this file is not getting included on the page, and the js functions included in the file are not available in console.

Could someone advise me how to include a js file so that it is available for all pages?

Thanks.

Upvotes: 0

Views: 5100

Answers (3)

Gev99
Gev99

Reputation: 300

if you are using laravel vue do this

place th common_js.js file in resources folder than change this

//in webpack.mix.js
mix.js(["resources/js/app.js","resources/js/common_js.js"], "public/js")

and in app.blade.php use this

<script src="{{ mix('js/app.js') }}" defer></script>

than

npm run dev

Upvotes: 1

Ahmed Hassan
Ahmed Hassan

Reputation: 659

you need to check the path rendered by asset() if it's not the correct path you can change the asset path from the .env file ASSET_URL

Upvotes: 0

Gev99
Gev99

Reputation: 300

run command

php artisan view:clear

Upvotes: 0

Related Questions