0

I pass php $var into Javascript from controller. $var has fetched addresses from DB. And i put it in Javascript. Now i can see the addresses in the console. No problem with that, I don't know why syntax error pop up too.

This is how I insert it into JS.

function initMap(){
        var options = {
            zoom:8,
            center:
                '{!! $estates[0]->address !!}'
        }

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

        var marker = new google.maps.Marker({
            position:
                    @foreach($estates as $addr)
                        '{!! $addr->address !!}',
                    @endforeach
            map:map
        });

        var infoWindow = new google.maps.InfoWindow({
            content:'content here'
        });

        marker.addListener('click', function () {
            infoWindow.open(map, marker);
        });
    }

my foreach running without a problem I can see the addreses in the console but also at this line: '{!! $estates[0]->address !!}' error pops up too. Actually I am seeing the address not this line.

error is this:

Uncaught SyntaxError: Invalid or unexpected token

Do you have any idea? am I making syntax mistake. But if do that then how can I retrieving the addresses at the console?

Also having this error too at the same line:

Undefined variable: estates (View: /var/www/html/laravel/resources/views/layouts/app.blade.php) (View: /var/www/html/laravel/resources/views/layouts/app.blade.php)

Controller

public function index()
{
    $estates = DB::table("allestates")
        ->get();
    return view("home", compact('estates'));
}

the topic is different the duplicated ones. it's not pure JS i am working with Laravel.

9
  • I think, php script won't work in javascript Commented Dec 4, 2018 at 8:35
  • @jin Javascript is inside Blade templates, so this should work. PHP in .js files won't work (unless you tell your server to) Commented Dec 4, 2018 at 8:37
  • Possible duplicate of How to pass variables and data from PHP to JavaScript? Commented Dec 4, 2018 at 8:42
  • it is surely not the same topic @Thomas! Commented Dec 4, 2018 at 8:44
  • @Snickers but the controller code too Commented Dec 4, 2018 at 8:56

2 Answers 2

0

I think one of the addresses contains the ' character. To avoid it use the addslashes function. You could do that in the controller:

public function index()
{
    $estates = DB::table("allestates")->get()->map(function($estate) {
        $estate->address = addslashes($estate->address);
        return $estate;
    });

    return view("home", compact('estates'));
}

And the according js would be:

var options = {
    zoom:8,
    center: new google.maps.LatLng({{$estates[0]->lat}}, {{$estates[0]->long}});
}

Because you have multiple addresses, it means you will have multiple markers too. That being said your code should look something like:

function initMap(){
    var options = {
        zoom:8,
        center: new google.maps.LatLng({{$estates[0]->lat}}, {{$estates[0]->long}});
    }

    var map = new google.maps.Map(document.getElementById("map"), options);
    var infoWindow = new google.maps.InfoWindow({
        content:'content here'
    });
    var markers = [];

   @foreach ($estates as $key => $estate)

    markers[{{$key}}] = new google.maps.Marker({
        position:  new google.maps.LatLng({{$estate->lat}}, {{$estate->long}});
        map: map
    });

    markers[{{$key}}].addListener('click', function () {
        infoWindow.open(map, markers[{{$key}}]);
    });

    @endforeach
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Snickers make sure that the estates variable is really named like that.. It seems to be some kind of typo
no problem with the name I checked it but yoru code not worked same error still there
thanks for update but now i am getting this error. InvalidValueError: setCenter: not a LatLng or LatLngLiteral: not an Object by the way there are 4 addreses in the database. if you delete foreach how can I itarate thoese with marker?
Well, now there is another story.. The center property expects an object with latitude and longitude set. You can't pass it an address string. Probably the position too. Also, the implode I wrote does the same thing as your foreach
I gave the center: city lat and lng but marker need to go with address string. Now error jump to the marker section.
|
-2

You can use php variables inside laravel blade files as

var options = { zoom:8, center: '{{$estates[0]->address}}' }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.