0

I have created a session array in laravel using the code:

Session::put("backUrl", array($workout_id =>URL::previous()))   ;
  //or
Session::push("backUrl.$workout_id", URL::previous())   ;

Both works and it has been created successfully and I could see it in the debugger

'backUrl' => array(1) [
    '78' => string (36) "http://192.241.4.104/admin/view?cs=1"
]

now I am unable to print it, The code i have used is

echo Session::get("backUrl"[$workout_id]);

it shows a syntax error, unexpected '[' error

And I have also used

echo Session::get("backUrl[$workout_id]"); 

nothing works

2 Answers 2

2

Because you keyed your entire array under the session variable "backurl".

if you var_dump:

var_dump(Session::get("backUrl")):

I am pretty sure you get:

array(
    [2] => "http://previous-url"
)

So wether you go like this:

$lastUrl = Session::get("backUrl");
echo array_keys($lastUrl)[0]; //workout-ID
echo array_values($lastUrl)[0]; //Value

Or you save your two variables seperately:

Session::put("backUrl", URL::previous());
Session::put("lastWorkoutId", $workout_id);

And then read them individually:

Session::get("backUrl");
Session::get("lastWorkoutId");
Sign up to request clarification or add additional context in comments.

4 Comments

i have a list of 100+ and still growing records so this will make me create `2n' sessions (i.e) 200+ if accessed all the ecords
Session is per user. Therefore each user will have one backUrl and one lastWorkoutId data, attached to their session data; not 200 sessions as you said.
but he could view any list so i need to set several sessions, thats why i am trying to use an array
You did not state that in your question but then you have to read the contents of the session as I told you in my first sample, save the array in a PHP variable and modify the array, apply some changes to the array (add or remove entries) and then and then save it again in your session overwriting the data of your session-key. This way you can handle an associative array in a single session-variable.
2

After several trials I have got what I wanted a session array for back button URL and thanks to @Steini for his valuable suggestions. I am posting this as it could be useful to someone...

At first I have changed using

Session::put("backUrl", array($workout_id =>URL::previous()))   ;

to

Session::put("backUrl.$workout_id", URL::previous())    ;

Saw the Session::push tag in Laravel Docs and tried luckly it worked. Because the first one deletes the existing session array and creates a new one.

And printing the Laravel session array is as simple as printing a session with added suffix

Session::get("sessionArrayName")['id']
   (i.e)
Session::get("backUrl")[$workout_id];

Thus got my session array printed and used it for my back button....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.