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

david19's avatar

Javascript Variable to @json

Hello Team, I have this function here:

eventClick:  function(info) {

            var publicId = info.event._def.publicId;
            var route = @json(route('shiftevents.edit'));

            window.location.href = route;
         },

The laravel route helper need the parameter "publicId". This will not work:

 var route = @json(route('shiftevents.edit',publicId));

The "publicId" come from javascript, not php. This is the problem.

0 likes
6 replies
LaryAI's avatar
Level 58

To pass the publicId variable from JavaScript to the Laravel route helper, you can use an AJAX request to send the publicId value to a PHP script that will return the route URL with the publicId parameter. Here's an example:

  1. Create a PHP script that accepts the publicId parameter and returns the route URL with the parameter:
// routes/web.php
Route::get('get-route-url/{publicId}', function ($publicId) {
    $routeUrl = route('shiftevents.edit', ['publicId' => $publicId]);
    return response()->json(['routeUrl' => $routeUrl]);
});
  1. In your JavaScript code, make an AJAX request to the PHP script to get the route URL with the publicId parameter:
eventClick: function(info) {
    var publicId = info.event._def.publicId;
    var url = '/get-route-url/' + publicId;
    $.get(url, function(data) {
        window.location.href = data.routeUrl;
    });
},

This code sends a GET request to the /get-route-url/{publicId} URL with the publicId value as a parameter. The PHP script returns a JSON response with the routeUrl value, which is used to redirect the user to the correct route with the publicId parameter.

MohamedTammam's avatar

if the route is yourwebsite.com/bla/{public_id} then do

let route = @json(route('shiftevents.edit')) + publicId;
1 like
Snapey's avatar

I've always had to put in the actual route and not use the route helper.

let route = '/bla/' + publicId + '/edit';
1 like
david19's avatar

@Snapey Yes this works, but its not pretty. If the route change, then there is a problem. Anyway i have worked solution, but its not clean. Yes, with livewire its very easy, but its okay. This works greate. You can see, i can use the route helper, for the "Create Route", but not for "Edit".

dateClick: function(info) {

            //livewire.emit('dateOnFullCalendarIsClicked',info.dateStr);
            var date = info.dateStr;
            var route = @json(route('shiftevents.create'));
            var requestParam = route.concat('?date=');
            var fullUrl = requestParam.concat(date);
            window.location.href = fullUrl;
        },

        eventClick:  function(info) {

            var publicId = info.event._def.publicId;
            var route = @json(url('/').'/dashboard/calendar-module/shiftevents/');
            var addIdToUrl = route.concat(publicId);
            var fullUrl = addIdToUrl.concat('/edit');
            window.location.href = fullUrl;
         },
Snapey's avatar

@david19 another option is to get the route in js and then overwrite a placeholder within the url;

var route = @json(route('shiftevents.edit','publicId'));

here the id is replaced by the string "publicId" which you could then string replace with the actual ID within JS

Please or to participate in this conversation.