Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vivekkumar10016's avatar

Reading Array variable from laravel 5.4 session using javascript

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.

0 likes
13 replies
topvillas's avatar

How are you passing it to your javascript?

You might need to use JSON.parse() as it's probably just a string.

vivekkumar10016's avatar
function addRoute(map) {
                var directionsService = new google.maps.DirectionsService();
                var directionsDisplay = new google.maps.DirectionsRenderer();
                directionsDisplay.setMap(map);
                @if(session()->has('waypts'))
                  var zoektermen_json = "{{!! json_encode(session()->get('waypts')) !!}}";
                  var zoektermen = JSON.parse(zoektermen_json);
                  console.log(zoektermen);
                @endif
                var all_points=[];

                     var request = {
                    origin: '{{Session::get('source')}}',
                    destination: '{{Session::get('destination')}}',
                    waypoints: [
                      {
                        location:'{{Session::get('waypts')[0]}}',
                        stopover:true
                      }
                    ],
                    optimizeWaypoints: true,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                };

                    directionsService.route(
                    request,
                    function(response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(response);
                        }
                    }
                );
            }


vivekkumar10016's avatar

I am getting all waypoints from table and then I am trying to put all waypoints in google maps api waypoints. but, I am always getting error. Uncaught SyntaxError: Unexpected identifier

Cronix's avatar

Is your javascript in a separate .js file, or is it in a laravel viewfile?

If it's in a javascript file, you can't use laravel blade syntax in there, like you are here: @if(session()->has('waypts'))

I'm thinking it's choking on the @ symbol (Uncaught SyntaxError: Unexpected identifier) bc javascript has no clue what that is.

Cronix's avatar

Also, the blade syntax is for unescaped data is {!! var !!}, not {{!! var !!}}

1 like
vivekkumar10016's avatar

I am using javascript in blade file. after changing {{!! var !!}} to {!! var !!} I am getting unexpected identifier error.

Cronix's avatar

Did you fix your blade syntax like I mentioned in my last post?

Cronix's avatar

That should have fixed the last error you posted (you deleted it) that mentioned the { symbol. What's the new issue?

vivekkumar10016's avatar

I am getting this error now.


Uncaught SyntaxError: Unexpected identifier
js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:40 Uncaught ReferenceError: addRoute is not defined
    at tg.<anonymous> (get:174)
    at tg.<anonymous> (js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:101)
    at _.Dv._.A.trigger (js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:100)
    at _.Dv.<anonymous> (js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:37)
    at _.Su._.A.trigger (js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:100)
    at _.Su.<anonymous> (js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:37)
    at Object._.A.trigger (js?v=3.exp&region=IN&language=en-gb&key=&libraries=places:100)
    at cv (common.js:142)
    at common.js:139
    at Hu.B (common.js:132)
vivekkumar10016's avatar

if I am trying this it gives me unexpected number:


var zoektermen_json = "{!! json_encode(session()->get('waypts'),JSON_FORCE_OBJECT) !!}";
vivekkumar10016's avatar
Level 1

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.