Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Loach's avatar
Level 11

Sending php variable to Jquery

I am using Jquery in my app and in the blade.php file I am trying to send the users id.

$(document).ready(function(){
    $.get( "/get_user_music", { id: @php $user->id @endphp } )
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

I am getting the following error in the console. What is the proper way to do this?

Uncaught SyntaxError: Unexpected token '}'
0 likes
2 replies
Tray2's avatar
Tray2
Best Answer
Level 73

Why are you passing an obejct to the get function?

Wouldn't it be easier to just

$.get( "/get_user_music/{{ $user->id }}")
Loach's avatar
Level 11

Thanks Tray2. I managed to get it working.

Please or to participate in this conversation.