Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display session data on view template in Laravel 5

I'm trying to display session data on a view template in Laravel 5. However it doesn't seem to show anything.

Here is the code i'm using to set the session:

Session::set('bookingConfirmed', BookingDates::where('id', Session::get('bookingFormData.slot.id'))->first()); 

Here is the code i'm using to display:

{{ Session::get('bookingConfirmed->time') }}
like image 402
V4n1ll4 Avatar asked Jun 02 '15 11:06

V4n1ll4


People also ask

How do I view a session in Laravel?

The session configuration file is stored at config/session. php . Be sure to review the well documented options available to you in this file. By default, Laravel is configured to use the file session driver, which will work well for many applications.

How pass data from one view to another in Laravel?

There are various ways of passing data to views:By using the name array. By using with() function. By using compact() function.

How do you access session variables in blade?

To access the session data, we need an instance of session which can be accessed via HTTP request. After getting the instance, we can use the get() method, which will take one argument, “key”, to get the session data.


2 Answers

I'm guessing it's because you've got the attribute of the object within the string that identifies the key of the object you want to get. I.e. move ->time as so:

{{ Session::get('bookingConfirmed')->time }}

You may also want to check it exists firstly too:

@if(Session::has('bookingConfirmed'))
    {{ Session::get('bookingConfirmed')->time }}
@endif
like image 91
haakym Avatar answered Sep 23 '22 11:09

haakym


You might want to try first getting the object and then get the fields of it like this

{{ Session::get('bookingConfirmed')->time }}
like image 39
Margus Pala Avatar answered Sep 22 '22 11:09

Margus Pala