Ad

Issue With Passing Id From Laravel Api To Select2 Script

- 1 answer

I have strange problem because previously in my app this code was working but now isn't. I take data from Laravel api via url address:

/api/customer/{id}/products

to select2 script

$('.js-data-example-ajax').select2({
    ajax: {
    url: '/api/customer/{id}/products',
    dataType: 'json',
    data: function (params) {
      var query = {
        q: params.term,
      }
    return query;
}

but Laravel make url address i that way:

api/customer/%7Bid%7D/products

so, I have %7Bid%7D instead of {id} and I'm looking for solution in google without success.

Ad

Answer

The character "7B" is { converter to asci, before the ajax request create a var call "url"

i call the route with his name, for give it the name just attach

->name('your_name') in the route file

and after in url variable i use the route name instead the full url

url = '{{ route("your_route_name", ":id") }}';

then replace the id placeholder with the id of select

url = url.replace(':id', id);

finally in ajax request

ajax: { 
    url: url, 

    //the rest of ajax request
}
Ad
source: stackoverflow.com
Ad