mam sobie taką metodę, którą wołam ajaxem
public function findLocation($customerId)
{
$locations = Location::where('customer_id', $customerId)->pluck('id', 'location_name');
return $locations->toJson();
}
to mi daje taki output
{"LOK_01":2,"LOK_066":3}
teraz JS
$( '#customer_id' ).on('change', function(e){
var id = $( this ).val();
var url = '/document/post/' + id;
$.get(url, function (data) {
$( '#location_id' ).select2( 'destroy' );
$( '#location_id' ).find( 'option' ).remove();
$( '#location_id' ).select2({
data: data
});
console.log(data);
});
});
rezultat jest taki, że owszem wypełniam select2 opcjami ale każdy znak z {"LOK_01":2,"LOK_066":3} jest w osobnej opcji. Generalnie źle wypełniam select2.