Please wait, I know you will say that this is a possible Duplicate. The answer is Yes. Here's the link
But none of the answers works for me.
On my blade I put the value in a hidden input field.
<input type="hidden" value="{{ $recipe->cuisine_type_id }}" id="selectedCuisineTypeId">
I have the same scenarios. Here's my JS;
var selectedCuisineTypeId = $("#selectedCuisineTypeId").val();
$('#cuisine_type_id').val(selectedCuisineTypeId).trigger('change');
$( "#cuisine_type_id" ).select2({
ajax: {
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
placeholder: "Select Cuisine",
url: "{{ route('ajax-cuisine-search') }}",
type: "post",
dataType: 'json',
delay: 250,
data: function (params) {
return {
search: params.term // search term
};
},
processResults: function (response) {
return {
results: response
};
},
cache: true
}
});
Why it is not working for me? What did I miss?