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

sesc360's avatar

Session Array

Hi, I got some issues with a session array I created:

        $incidentReference = TicketSystem::generateIncidentReference();
        $incidentID = TicketSystem::generateIncidentID();

        // Save incidentID to Session
        $incidentData = ["incidentID" => $incidentID, "incidentReference" => $incidentReference];
        Session::push('incidentData', $incidentData);

I use an AJAX call to get the Session Information to update a form. The laravel function returns the Session data to this AJAX call which is handled by a callback. This works fine. But when I log the resulting data in the console, I only see this:

Created Incident ID: [object Object]

So I would think I would need to access the array with something like data[0] But this does not work.

  1. how would I access elements of an array within a session? Is it as well Session::get('test')? or is it differently?
0 likes
2 replies
rodrigo.pedra's avatar

I don't quite understand if your problem is in the backend side (laravel) or in the frontend side (javascript).

If you want to access an inner key from an array stored in a session you can use the dot-notation:

// app/Http/routes.php
$router->get( '/test',  function (  ) {
    $incidentData = ['incidentID' => 1, 'incidentReference' => 'ref'];
    \Session::set('incidentData', $incidentData); // use set() not push()

    return \Session::get('incidentData.incidentID'); // will return 1
} );

If you return \Session::get('incidentData'); Laravel will send a JSON object as the value is an associative array. So in this case:

// app/Http/routes.php
$router->get( '/test',  function (  ) {
    $incidentData = ['incidentID' => 1, 'incidentReference' => 'ref'];
    \Session::set('incidentData', $incidentData);

    return \Session::get('incidentData'); // will return { "incidentID" : 1, "incidentReference" : "ref" }
} );

and in your view

<script>
    $(function () {
        'use strict';

        // using jquery for simplicity
        $.get('./test' ).success( function ( incidentData ) {
            alert( incidentData ); // will alert [object Object]
            alert( incidentData.incidentID ); // will alert 1
        } );
    });
</script>
bashy's avatar

If the above doesn't work for you, please post the JavaScript part so we can see where you're going wrong.

Remember, there's different responses

function(response, status, xhr) {}

Please or to participate in this conversation.