3

Hello everyone I'm new to Laravel and I'm currently using Laravel 6.0

For some reason my javascript using @push is not working. The script only works when I paste the code in the blade file and not using the asset folder.

add.blade.php

@extends('layouts.app')
@section('content')
// Long HTML code
@endsection

@push('scripts')
<script src="{{ asset('js/position.js') }}"></script>
@endpush

layouts > app.blade.php

 <main class="py-4">
      @yield('content')
    </main>
  </div>
  @stack('scripts')
</body>

public(asset) > js > position.js

 $('#department').change(function(){
    var departmentID = $(this).val();    

        if(departmentID) {
        $.ajax({
        type:"GET",
        url:"{{url('get-section-list-position')}}?department_id="+departmentID,
        success:function(res){               
            if(res) {
            $("#section").empty();
            $("#section").append('<option>Please Select Section</option>');
            $.each(res,function(key, value){
            $("#section").append('<option value="'+key+'">'+value+'</option>');
          });
          } else {
            $("#section").empty();
          }
        }
      });
    } else {
        $("#section").empty();
    }      
  });

If I paste the script in the add.blade.php and used @section, it works but I'm afraid that the security of my system would be compromised. Please help.

17
  • Does the console show any thing in regards to your script? Commented Sep 24, 2019 at 8:11
  • 1
    So the script doesn't show up in the source code or does it just not run? Do other scripts work when being @pushed? Do you get any errors in your browser console? Does your add.blade.php extend layouts/app.blade.php? Commented Sep 24, 2019 at 8:11
  • @Script47 Nope it doesn't show anything. But I tried using console.log('hello') it appears in the console. Commented Sep 24, 2019 at 8:19
  • 1
    But one thing, why are you doing this ? lets say if you have code in your position.js this page is also for more stuff as well, so if you want to use it, then it will also be shown inside page view source, and maybe it may slow down your page performance as well. Commented Sep 25, 2019 at 6:08
  • 1
    The push works like it throws a piece of code from every page to where you have loaded it. So, it reduces the performance issues on that specific page. Commented Sep 25, 2019 at 6:10

1 Answer 1

1

Your Javascript won't work as standalone because it won't get parsed by Blade, but it contains {{url('get-section-list-position')}}. Either change {{url('get-section-list-position')}} to the "hardcoded" URL or place your Javascript code inside your @push('scripts') if you want to use Blade directives:

@push('scripts')
<script>
$('#department').change(function(){
    var departmentID = $(this).val();    

        if(departmentID) {
        $.ajax({
        type:"GET",
        url:"{{url('get-section-list-position')}}?department_id="+departmentID,
        success:function(res){               
            if(res) {
            $("#section").empty();
            $("#section").append('<option>Please Select Section</option>');
            $.each(res,function(key, value){
            $("#section").append('<option value="'+key+'">'+value+'</option>');
          });
          } else {
            $("#section").empty();
          }
        }
      });
    } else {
        $("#section").empty();
    }      
  });
</script>
@endpush
Sign up to request clarification or add additional context in comments.

4 Comments

This works just like with @section. But I have a question doing this, will it make the system vulnerable? It seems like this can be seen in a view page source.
Not more than it would using an external javascript, which can also be viewed in your browser.
You're right, I just complicate things. I think I will stick to this method. Thanks @kerbholz!
Not a problem, glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.