How are you passing it to your javascript?
You might need to use JSON.parse() as it's probably just a string.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey everyone, I need a help. I am storing one array variable in laravel session but while reading it using javascript, I am not able to read. if I fetch index wise i am able to fetch but I am not able to loop through.
Thanks I could solve it. This is how I solved.
function addRoute(map) {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
var zoektermen_json;
var waypts = [];
@if(session()->has('waypts'))
zoektermen_json = {!! json_encode(session()->get('waypts'),JSON_FORCE_OBJECT) !!};
for(var property in zoektermen_json) {
waypts.push({location:zoektermen_json[property],stopover:true});
}
@endif
//directionsDisplay.setPanel(document.getElementById('panel'));
var request = {
origin: '{{Session::get('source')}}',
destination: '{{Session::get('destination')}}',
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
{{--var route = {!! $route !!};--}}
{{--route.request = request;--}}
{{--directionsDisplay.setDirections(route);--}}
directionsService.route(
request,
function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
}
);
}
Please or to participate in this conversation.