Did you try '''dd(session()->all());''' ?
Session variables not saving / persisting after redirect
Comment.php
public function store($data)
{
$comment = new Comment;
$comment->title = $data->title;
$comment->body = $data->body;
$comment->save();
session(['commentTitle' => $data->title]);
session(['commentBody' => $data->body]);
dd(session('commentTitle')); // This dumps the title string as expected...
return redirect()->action('CommentsController@index');
}
CommentsController.php
public function index()
{
$comments = Comment::get();
dd(session('commentTitle')); // This dumps null...?
return view('/comments/index')->with([
'comments' => $comments,
]);
}
Any idea why the session variables are not persisting?
There is might be one of the below your solution.
1) Save the session, and check
public function store($data)
{
$comment = new Comment;
$comment->title = $data->title;
$comment->body = $data->body;
$comment->save();
session(['commentTitle' => $data->title]);
session(['commentBody' => $data->body]);
dd(session('commentTitle')); // This dumps the title string as expected...
//Saving the session...
Session::save();
return redirect()->action('CommentsController@index');
}
If solution 1) still facing the same then try 2):
2) Middleware side setup
Check you app/kernal.php for \Illuminate\Session\Middleware\StartSession::class, and add it into protected $middleware array. Remove from protected $middlewareGroups if exist
Hope this work for you.
@saurabh moving the StartSession::class to the middleware array worked for me, but I really want to know why, and what that changes. Can you add that to your answer?
Oh, FYI.. the kernel.php file is at app/Http/kernel.php
@saurabhd Thank you really much, the 2nd solution worked for me :) Hope it will help someone else :)
@bgies Here($middlewareGroups) its applied to a specific middleware group i.e web and here($middleware) its applied to every request. Though the issue i'm facing on the route is on web middleware group so it doesnt make sense moving it to global middlewares list.
Agree with da3monhunt3r
I'm using 5.7 and also face with this issue. Even I added in $middleware and removed from $middlewareGroups still not work
session data will not be persisted if you dd in your code since writing to session store is performed in the terminable middleware.
In my case, session was not saved while dump() was used. I didn't imagine dump() has such a destructive impact.
@Snapey thanks a lot!
@BHiko i have noticed the same in my situation, however it seems to only be a problem when authentication is attempted. @BHiko did you find why this was happening in your case/manage to fix it? And @Snapey would you have any idea about what might be causing this behaviour?
Just to clarify i am putting a dump(session()->all()) not a dd() in the code. Speicifically, I have put this just before the return in th Illuminate\Foundation\Http\Kernel::handle() function.
@saurabhd thanks this worked for me
Hello all. I have same problem. I'm struggling with Session. This is my code:
class randomermesController extends Controller
{
public $maxInterv=10;
public $interventi;
public function data(Request $req){
echo("costruisco<br>");
//dd(Session::all());
if(Session::has('interventi')){
$this->interventi = Session::get('interventi');
echo("sessione esistente!<br>");
}
else{
$this->interventi=new EventiClass($this->maxInterv);
for($i=0;$i<$this->maxInterv;$i++){
$this->interventi->add();
}
Session::put('interventi',$this->interventi);
Session::Save();
echo("sessione nuova<br>");
}
for($i=0;$i<$this->maxInterv;$i++){
echo($i." - ");
echo($this->interventi->items[$i]->inizio->format("d/m/Y h:i:s"));
echo(" - ");
echo($this->interventi->items[$i]->fine->format("d/m/Y h:i:s"));
echo("<br>");
}
return("ciao");
}
}
class EventiClass
{
public $total,$maxInterv,$items;
function __construct($maxInterv){
$this->total=0;
$this->maxInterv=$maxInterv;
$items=array();
}
function add(){
$dest=-1;
if($this->total<$this->maxInterv){
$dest=$this->total;
}else{
for($i=0;$i<$this->maxInterv && $dest!== -1;$i++)
{
if($this->items[$i]->concluso) $dest=$i;
}
}
if($dest>-1){
$dataMin=new DateTime('NOW');
if($this->total>0){
for($i=0;$i<$this->total;$i++){
if($this->items[$i]->inizio > $dataMin) $dataMin=$this->items[$i]->inizio;
}
}
$this->items[$dest]=new EventoClass($dest,$dataMin);
if($this->total<$this->maxInterv) $this->total++;
}
}
}
class EventoClass
{
public $inizio;
public $fine;
public $indice;
function __construct($i,$dataMin){
$this->indice=$i;
$this->inizio=$dataMin;
$add=rand(1,5);
$this->inizio->modify('+'.$add.' min');
$this->fine=new DateTime('NOW');
$add2=$add+rand(2,40);
$this->fine->modify('+'.$add2.' min');
return $this;
}
function concluso(){
$adesso= new DateTime('NOW');
if($adesso > $this->fine) return true;
return false;
}
}
but session is resetted to every page reload! I also made modify in middleware like saurabhd says:
* @var array
*/
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Illuminate\Session\Middleware\StartSession::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
but it doesnt work! it's incredible! plese help me. bye.
I made this:
public function data(Request $req){
print_r($req->session()->all());
and I obtain for any reload a token that is different from before:
first reload:
Array ( [_token] => ptQzilDr0q393hBOblwwxmn6Rrxl3GflHylNCyCT )
second reload:
Array ( [_token] => RmWuoQYin8UceyxyhBmTrov1g8WQGcbM64BdNMQt )
and so on
For me, and I suspect for others as well, the problem is putting a dd in there...the session gets persisted at the end of the request lifecycle...die and dump ends the lifecycle before that happens. Here is a better, more in depth explanation:
https://stackoverflow.com/questions/23917409/laravel-session-data-not-sticking-across-page-loads
moving the \Illuminate\Session\Middleware\StartSession::class to middleware from middleware group worked for me !
Please or to participate in this conversation.