1

I am trying to fetch locations from DB table. First using controller to keep data in $var

public function index()
{

    $adresArray = DB::table("allestates")->select('id','lat','lng','price')->get()->toArray();

    return view('home', compact('adresArray'));


}

and then passing those to the blade

    var locations = @json($adresArray)

    function initMap()
    {
        var options =
            {
                zoom : 10,
                center : {lat:34.652500, lng:135.506302}
            }

        var map = new google.maps.Map(document.getElementById('map'), options);

        var markers = locations.map(function(location, i) {
            var m = new google.maps.Marker({
                map: map,
                position: {
                    lat: location.lat,
                    lng: location.lng
                }
            });

            var infoWindow = new google.maps.InfoWindow({
                content: location.price
            });

            m.addListener("click", function() {
                infoWindow.open(map, m);
            });
        });

    } 

Undefined variable: location

What am I missing here?

1 Answers1

0

Use this sintax inside js scripts in blade:

var addresses = {!! $location !!};

Also, looking at your controller, you are not getting a single object so you have to make an iteration over location array to work with every object inside, you can't just place {!!$location->lng !!} like you are doing...

Walter Cejas
  • 1,924
  • 1
  • 12
  • 22
  • But in the end how can I iterate lat and lng in the google map? –  Dec 10 '18 at 04:22
  • check for this answer please https://stackoverflow.com/a/3059129/5458355 – Walter Cejas Dec 10 '18 at 04:24
  • I updated the code in the question. but still no effects, getting undefined var error. –  Dec 10 '18 at 04:49
  • in your last update you're passing estatesArray in your compact function but the variable name is $adresArray – Walter Cejas Dec 10 '18 at 05:02
  • I am sorry for typo, already fix it but result is still same. –  Dec 10 '18 at 05:06
  • Place debugger; after the declaration of variable locations, debug it and post the content of that variable, – Walter Cejas Dec 10 '18 at 05:12
  • Maybe is coming null from controller – Walter Cejas Dec 10 '18 at 05:13
  • it can't be because, I tried to pass data into JS directly {{location->lat}} and use @foreach for itaration. it went well i saw the markers with location in the map. But another problem popped up. That's why i try use this way and trying to pass $php var to JS var –  Dec 10 '18 at 05:19
  • yes but can you pass me the contents of the var locations, before the function initMap() or its null ? if not, chat me – Walter Cejas Dec 10 '18 at 05:27
  • I am sorry for the burden. I can't debug it because I am getting undefined error! https://imgur.com/a/LrNoFTJ and don't worry about the $estatesArray it's same in controller too. –  Dec 10 '18 at 05:37
  • The view to return maybe should be layouts.app, not home.. push your proyect to a github repo .. – Walter Cejas Dec 10 '18 at 05:43
  • Thank you for helping me out. But it'll take a time pushing the project. –  Dec 10 '18 at 06:01