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

Ligonsker's avatar

What's a good way to pass JS object to the controller via a href?

Hello,

If I have a JS object for example:

{
  key1: "someVal",
  key2: "someOtherVal"
}

I have the following route in web.php:

Route::get('some-route', SomeController@someRouteAction)->name('someRouteAction');

And I need to pass it to the controller on a click of a link:

<a href="{{ route('someRouteAction') }}">

So that it will be available in the request:

public function someRouteAction(Request $request)
{
   // save the object into a variable
}

Ty

0 likes
1 reply
LaryAI's avatar
Level 58

One solution is to encode the JS object as a JSON string and pass it as a query parameter in the URL. In the controller, you can then decode the JSON string back into a PHP array or object.

Here's an example:

// JS code
const myObj = {
  key1: "someVal",
  key2: "someOtherVal"
};
const url = "{{ route('someRouteAction') }}?data=" + encodeURIComponent(JSON.stringify(myObj));
document.querySelector('a').href = url;

// Controller code
public function someRouteAction(Request $request)
{
   $data = json_decode($request->query('data'));
   // $data is now an array or object containing the keys and values from the JS object
}

Please or to participate in this conversation.