0

I have this very simple class:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function __construct()
    {
//        $this->middleware('auth');
    }

    public function home(Request $request)
    {
        echo "setting help key";
        session()->put('help', 'me');
        session(['sos' => 'me']);
        dump(session('help'));
        dump(session('sos'));
    }

    public function home2(Request $request)
    {
        dump(session('help'));
        dump(session('sos'));
    }
//...

which dumps the vars successfully in the home() page, but when i access to the home2() page, it fails. Maybe it has something to do with me disabling the default middleware('auth'), but i'm not sure (also, if that is the case, how to use sessions without forcing the login)

1 Answers1

2

try

session(['key' => 'value']); // to store
session()->get('key'); // to get the value
session()->forget('key'); // to unset the session attribute

read also: https://laravel.com/docs/5.8/session (change the Laravel version at the top-right corner...)


P.S. Sometimes you can't just dump data. Try var_export or what's better, just debug.

LowLevel
  • 1,085
  • 1
  • 13
  • 34
  • As you can see in the code i added in my question, i´ve already tried with the syntax commented in the docs to store, as well as the one to get the value. I even tried another one not very explicit in the docs Also, if a variable´s dump returns `null`, then there´s no point in trying to "debug" it with another function. And more, if the framework provides apparently simple ways to use a session, then it shouln't be so difficult to see what's in it. PD: have you tried the code i posted? Did you had the same issue, or it worked ok? – Noestoy Disponible Jan 02 '20 at 22:34
  • I've never heard of `dump`, actually. I used `var_dump`. Try and see what happens. Maybe your `dump` function destroys the session or clears all attributes. I'm not sure. I'm not a PHP developer. However, I'm familiar with. – LowLevel Jan 03 '20 at 11:58
  • Look, the problem is, I have made Laravel applications too, and the sessions work just as expected. Every single Laravel-developer will state for sure that sessions work. That's not a magic and it is just a fact. I just have had a similar problem when I stored and got session attributes in other way than I wrote in my answer. That's why I thought you had same issues. – LowLevel Jan 05 '20 at 00:12
  • Because it is not that clear what happens in your code (maybe you redirect from post to get = PGR pattern) it is not easy to find out the problem you have. It is possible that you will see what's in your session if you write `var_dump(session()->get('key'); die;`. However, it is not excluded that even in that case you won't see anything. That's why I would prefer to debug. – LowLevel Jan 05 '20 at 00:14
  • When you redirect you can also write: `redirect('route')->with('key', 'value');` this is the way you show kind-of post reports (that are unset automatically from the session. – LowLevel Jan 05 '20 at 00:17
  • Sometimes you need to change some folder permissions or configure your application for sessions. However, I can guarantee that sessions in Laravel work as intended. I mean, try to find a solution instead of trying to prove that sessions don't work in Laravel. – LowLevel Jan 05 '20 at 00:21