Im having trouble with my pagespeed in Laravel, because of the javascript in my page view.
Im trying to make my pagespeed better by not having any javascript on the page view, and instead in a seperate search.js file.
Im generating a map with google maps, and putting about 100 markers on the map. The data depending on where the markers should be placed are stored in a MySQL database (as part of the users information).
Right now im having a script on the page within a foreach loop on the page for each user passed to the view. This makes the page source code extreamly long, as it goes trough the loop 100 times and displayed all this javascript in the source code.
Below is only an example of my code to get the jist of what I mean, whole code is displayed here.
<script>
@foreach($usersall as $user)
...
var latlng{{ $user->id }} = {lat: {{ $user->lat }} , lng: {{ $user->long }} };
var marker{{ $user->id }} = new google.maps.Marker({
map: map,
clickable : true,
icon: '/assets/img/pin_marker_open.png',
position: latlng{{ $user->id }}
});
markers.push(marker{{ $user->id }});
...
@endforeach
</script>
I cant do that in my search.js file, because I dont have the data from $usersall.
So here goes my question: How do I pass the PHP variables for a user to the search.js file, so I can display all the markers on the map without having any of the javascript in the page view itself?
<script>tag in a.blade.phpfile, then you can use{{ }}, but if it's in a standalone.jsfile, you can't use that syntax. As suggested below, settingusersin a var before loadingsearch.jswould work, but you'd have to adjust your variable names and remove that syntax.