Home > other >  Can I use JS variables to access Laravel data? How should I write it?
Can I use JS variables to access Laravel data? How should I write it?

Time:08-26

let name = $(this).attr("name");
let value = "{{ $response['"   name   "'] }}";

I am running this in $(document).ready()

CodePudding user response:

Your code shouldn't work, because the php code will only be compiled first and then returned to the client(haha,That's correct, right?); Let js receive php variable value in blade

let value = `{{$a}}`;//`{{$a['hello']}}`

CodePudding user response:

Larvel has a helper to include json to be used in js https://laravel.com/docs/9.x/blade#rendering-json

  • Related