Reputation: 3635
I have a function for post and in I want to add an array to a session and use that in other page. But after one run it returns null:
$arr = [1,2,3];
session::put("a",$arr);
$b = session::get("a");
...
session::put("a",$b);
dd($b);
=> [
0=> 1,
1=>2,
2=>3
]
Now in next send post I comment two first lines:
//$arr = [1,2,3];
//session::put("a",$arr);
$b = session::get("a");
...
session::put("a",$b);
dd($b);
=> null
Why is it returning a null?
Upvotes: 0
Views: 229
Reputation: 196
You have to understand about the lifecycle of the session in laravel. Based on your code, you tried to dump the data before the lifecycle finish. What you should do is just let the lifecycle finish first by return the response and then you can get the data on your next request by dump the data or return to a response, so you don't need to stop the lifecycle.
Upvotes: 0
Reputation: 1372
You can't store an array in a session. You can do this with json_encode and json_decode. Example:
$arr = [1,2,3];
session::put("a",json_encode($arr));
$b = session::get("a");
Then, in the other page, you can do:
$b = json_decode(session::get("a"), true);
...
dd($b); // should print an array.
If you want an object intead an array, remove the second parameter: true, to json_decode.
Check these example in PHP Docs http://php.net/manual/en/function.json-decode.php
Upvotes: 1