laravel | How to access session array in JavaScript?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel | How to Access Session Array in JavaScript: Bridging PHP Data to the Frontend As a senior developer working with the Laravel ecosystem, we frequently encounter the challenge of bridging server-side data—like session variables or complex arrays—to the client-side JavaScript environment. When you pass an array from your controller to a view, the way you structure that data for consumption by JavaScript is crucial. If you try to nest PHP logic directly into Blade syntax expecting it to behave like a standard object in JavaScript, you often run into unexpected errors. This post will dissect the specific issue you encountered while trying to access nested session data and provide a robust, developer-approved solution using Laravel's features. ## The Pitfall: Mixing Server Logic with Frontend Interpolation The error you received—`Trying to get property of non-object`—occurs because you are attempting to chain methods like `Session::get('notification')->alert_type` directly within Blade syntax (`{{ ... }}`). Blade processes this as PHP code first, and when it tries to interpret the result into a string for JavaScript, it fails because the structure doesn't align with how session data is being retrieved or how it should be serialized. When you use `with('notification', $notification)`, Laravel stores that array in the session. To safely expose complex PHP arrays to JavaScript, you must serialize them into a universally recognized format: **JSON**. ## The Correct Approach: Serializing Arrays as JSON The most reliable way to pass an entire session array to a JavaScript variable is to use the Blade syntax to dump the entire PHP array directly into a JSON string. This allows the client-side code to parse the data cleanly, regardless of its internal structure. Here is how you should modify your view file: ### Step 1: Accessing and Serializing the Session Data Instead of trying to drill down into individual keys within the Blade expression, retrieve the entire session data that you need and wrap it in JSON. ```html ``` ### Why this works (The Developer Perspective) 1. **`@json(...)` Directive:** The `@json()` directive in Blade is specifically designed to take a PHP variable or expression and output its JSON representation. This ensures that whatever complex structure you pass from the server is converted into valid JavaScript object syntax (`{...}` or `[...]`) before it hits the browser. 2. **Safety:** By serializing the entire array, you avoid the ambiguity of trying to access nested PHP properties directly within the Blade interpolation. The data integrity is maintained because the transformation happens on the server (Blade rendering) before being sent to the client. This principle of separating presentation logic from data handling is fundamental to good application architecture, much like the principles promoted by the **Laravel** philosophy found at [https://laravelcompany.com](https://laravelcompany.com). 3. **Client-Side Control:** Once the data is in a clean JSON format, your JavaScript code gains full control over how it accesses and manipulates the data, which is much more robust than relying on complex server-side string manipulation for simple data transfer. ## Best Practices for Data Transfer When transferring data from Laravel to JavaScript, always prioritize clear, structured formats: * **For Simple Values:** If you only need one or two values (e.g., a single success message), pass them separately using standard Blade interpolation: `var message = "{{ session('notification.message') }}";`. * **For Complex Objects/Arrays:** When passing an entire collection or notification set, use `@json()` to ensure the data is perfectly formatted for consumption by modern frontend frameworks and vanilla JavaScript. By adopting this JSON serialization technique, you eliminate runtime errors caused by mismatched data types and create a predictable, maintainable flow between your Laravel backend and your frontend application. ## Conclusion Accessing session arrays in JavaScript requires careful serialization. The error occurred because direct chaining of PHP methods inside Blade interpolation is not the intended way to serialize complex data. By leveraging `@json(session('key'))`, you transform your server-side array into a safe, usable JavaScript object. This practice ensures that your Laravel application remains robust, secure, and easy to maintain, aligning perfectly with modern development standards promoted by the **Laravel** community at [https://laravelcompany.com](https://laravelcompany.com).