Tanvir Sourov
Tanvir Sourov

Reputation: 417

Multiple Parameters to Laravel Route in Ajax Success

I want to create dynamic buttons in Laravel after Ajax Success Method. I have been able to do that as well. But the problem arises when I try to pass the route in the anchor tag. I am adding the full JavaScript code for ease of understanding:

var data = JSON.parse(data);
var finalData = '';
var search_body_data =  $("#searchTableBody");
for (i = 0; i < data.length; i++) {
     finalData = finalData + ` <tr> <td> ${data[i]['data_id']} </td> `;
     finalData = finalData + ` <td> ${data[i]['type']} </td> `;
     finalData = finalData + ` <td><a class="btn btn-primary btn-block" href="{{ route('notification_detail',['type'=>data[i]['type'],'doc_id'=>str_replace('/','-',data[i]['data_id']),'part'=>"0"]) }}">Detail</a></td> `;
     finalData = finalData + ` </tr> `;
}
search_body_data.html(finalData);

But this gives me the following error: Use of undefined constant data - assumed 'data'. I have also tried:

${'type'=>data[i]}

But it also gives the same error. Also tried some other variations but still no luck. Any work around will be highly appreciated.

Upvotes: 0

Views: 487

Answers (1)

linktoahref
linktoahref

Reputation: 7972

Workaround would be to define your base route in a variable and append the parameters in the success callback function

var data = JSON.parse(data);
var finalData = '';
var base_url = "{{ url('notification_detail') }}";
var search_body_data =  $("#searchTableBody");
for (i = 0; i < data.length; i++) {
     finalData = finalData + ` <tr> <td> ${data[i]['data_id']} </td> `;
     finalData = finalData + ` <td> ${data[i]['type']} </td> `;
     finalData = finalData + ` <td><a class="btn btn-primary btn-block" href=" `+ 
                    base_url + `/`+ data[i]['type'] + `/` + 
                    data[i]['data_id'].replace('/', '-') +
                    `/0>Detail</a></td> `;
     finalData = finalData + ` </tr> `;
}
search_body_data.html(finalData);

Upvotes: 1

Related Questions