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

Tayyabshahzad's avatar

Passing Id to laravel named route

Hi,

How can I pass my Id (Primary key in below Ajax response ) My Route name is refundDetail this route will accept the Id parameter but I can't find the proper way to pass id please guide me.

	                $.each (data, function (key, value) {
                    res +=
                    '<tr>'+
                        '<td>'+value.id+'</td>'+
                        '<td>'+value.id+'</td>'+
                        '<td>'+value.billing_subtotal+'</td>'+
                        '<td> <a href="{{route('refundDetail',"'+value.id+'")}}"> <i class="fa fa-eye"></i> </a> </td>'
                    '</tr>';
                });
0 likes
10 replies
Nakov's avatar

you cannot do it that way, because the php code is server side, while javascript is client side. So you better put the route in, or use a library like this one: https://github.com/tighten/ziggy

This is what I mean by using the url:

<a href="/detail/refund/"'+value.id+'"> <i>...

or whatever the endpoint is.

tykus's avatar

@Tayyabshahzad why not return a view partial from the XHR request rather than building HTML in Javascript???

tykus's avatar

@Tayyabshahzad you mentioned already that you have an AJAX response, so for that request, why not respond with HTML instead of JSON?

tykus's avatar

@Tayyabshahzad I understand that. What you don't seem to understand is Blade has already rendered - so the route helper method is useless in the context of your Javascript script.

Tayyabshahzad's avatar

@Nakov if I add my route with static id then it's working fine for me but can't add the dynamic Id coming in value.id variable

'<td> <a href="{{route('refundDetail',1)}}"> <i class="fa fa-eye"></i> </a> </td>'
Tayyabshahzad's avatar
Tayyabshahzad
OP
Best Answer
Level 1

Finally i got the solution

Use this sayntax

  '<td> <a href="{{route('refundDetail',"")}}/'+value.id+'"> <i class="fa fa-eye"></i> </a> </td>'

Please or to participate in this conversation.