How Can I pass the variable (stock.id) return from Ajax response to the route to generate the url to edit a stock
$.ajax({ url: 'sectors/stocks/' + $(this).data('sector-id'), dataType:'json', beforeSend:function() { $('.stocks_list').html('Loading...'); } }) .done(function(data) { $('.stocks_list').html('<ul>'); $.each(data, function(index, obj_data) { $.each(obj_data.stocks, function(indx, stock) { $('.stocks_list').append('<li><a href="{{route("admin.stocks.edit","'+stock.id+'")}}">' + stock.symbol + ' </a></li>'); }); }); })
You can first use a placeholder to generate the URL with and then replace that in javascript.
var url = '{{ route("admin.stocks.edit", ":id") }}'; url = url.replace(':id', stock.id); $('.stocks_list').append('<li><a href="'+url+'">' + stock.symbol + ' </a></li>');
In my opinion the simplest way is by concatenating javascript variable with blade string as follows.
Case 1: Route paramter is required
In this case pass the empty string in place of route parameter to by pass the required validation.
var url = "{{route('admin.stocks.edit', '')}}"+"/"+stock.id;
Case 2: Route paramter is optional
In this case you do not have to pass the empty string.
var url = "{{route('admin.stocks.edit')}}"+"/"+stock.id;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With