0

i have a problem about passing variable from controller to javascript

this is my controller

public function index()
{
    $tourplace = Tourism::all();

    return view('spatialInfo')->with('tourplace',$tourplace);
}

this is my javascript

$(document).ready(function(){
        var lokasi = [];
        var test = document.getElementsByName('kabupaten');

        var jsondata = JSON.parse({!! $tourplace !!});
        var htmlURL = '{{URL::to('/tourismplace/id/')}}';
        $.get(jsondata).success(function(data){
            var jumlahData = data.length;
            for(var i=0;i<jumlahData;i++){
                lokasi.push({
                    lat : data[i].latitude,
                    lon : data[i].longitude,
                    zoom : 15,
                    title : data[i].nama,
                    html : "<a href='"+htmlURL+"/"+data[i].id +"'>"+data[i].nama+"</a>",
                    icon: 'http://maps.google.com/mapfiles/ms/micons/red-dot.png', // custom icon
                    animation: google.maps.Animation.DROP
                });
    }
});
rinaldy31
  • 117
  • 2
  • 9

1 Answers1

0

Instead of trying to parse JSON with JSON.parse() use this code:

var jsondata = {!! $tourplace->toJson() !!};

->toJson() will convert Your collection to JSON, without needing to parse it again.

Giedrius Kiršys
  • 5,154
  • 2
  • 20
  • 28