close

Laravel, right way to import javascript into Blade Templates

Hello Guys, How are you all? Hope You all Are Fine. Today we are talking about Laravel, right way to import javascript into Blade Templates in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Laravel, right way to import javascript into Blade Templates Error Occurs?

Today we are talking about Laravel, right way to import javascript into Blade Templates in php.

How To Solve Laravel, right way to import javascript into Blade Templates Error ?

  1. How To Solve Laravel, right way to import javascript into Blade Templates Error ?

    To Solve Laravel, right way to import javascript into Blade Templates Error Try to move pizza.js at the bottom of index.blade.php

  2. Laravel, right way to import javascript into Blade Templates

    To Solve Laravel, right way to import javascript into Blade Templates Error Try to move pizza.js at the bottom of index.blade.php

Solution 1

For custom scripts on specific page,

  • add @yield('footer-scripts') to layouts/app.blade.php
  • create a folder named 'scripts' in views folder
  • inside views/scripts folder create a file ‘pizza-script.blade.php‘ and add the js file contents inside<script src="/path/to/pizza.js" /> <!-- it might not work if path isn't clear --> <script type="text/javascript"> console.log('executing js here..') js file contents.... #if jquery needed add it like, $(document).ready(function(){ ... } </script>
  • in your index.blade.php (or the page where you want the scripts to execute) at the end add the script after @endsection @section('footer-scripts') @include('scripts.pizza-script') @endsection

now refresh the page, you can see 'executing js here..' on the console.

Solution 2

Try to move pizza.js at the bottom of index.blade.php

    @section('extra-script')

    {{Html::script('js/components/pizza.js')}}

    @endsection

Hope it helps.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment