It should be part of the request parameters. You have a post, not a get.
Sep 6, 2021
8
Level 2
How to use javascript variable in a laravel route?
I have a search function that returns the basic data of a user along with a generated button that should call a laravel route with the returned ID(value.id) as parameter when clicked. How do I use the value.id as a laravel parameter?
I tried creating a variable $newId and using it as a parameter but it doesn't work...
$('body').on('keyup','#search',function(){
var keyword = $(this).val();
$.ajax({
type: "POST",
url: "{{ route('search')}}",
dataType: "json",
data: {keyword: keyword,
_token: '{{csrf_token()}}'
},
success: function(res){
var tableRow='';
$('#dynamic-row').html('');
$.each(res, function(index, value){
var $newId = value.id;
tableRow = '<tr><td><b style="color: #bb2124">'+value.id+'</b></td><td>'+value.fname+' '+value.lname+'</td><td>'+value.contactNo+'</td><td><a class="btn btn-outline-success" id="viewUserId" href="{{ route("show", '+$newId+') }}"><b class="fas fa-info"></b> <b>VIEW INFO</b></a></td></tr>';
$('#dynamic-row').append(tableRow);
});
}
});
});
Please or to participate in this conversation.