A
A
Apostol632020-02-11 15:26:33
Laravel
Apostol63, 2020-02-11 15:26:33

Why is the session not being saved to the Laravel database?

Good day everyone!

I am writing here because I don’t understand at all what I did wrong or didn’t do at all.
It is necessary to save the session to the database after user authentication.
Further, everything is as described in the documentation:

LoginController and others are created via php artisan make:auth
Table sessions via migrations
--php artisan session:table
--php artisan migrate

session.php file

'driver' => env('SESSION_DRIVER', 'database'),
'connection' => env('SESSION_CONNECTION', null),
'table' => 'sessions',


created SESSION_CONNECTION in the .env file and assigned the value to mysql (Although nothing is said about SESSION_CONNECTION in the documentation)

here are the routes
Route::group(['middleware' => ['web']], function () {
    Route::get('/sess', 'Auth\[email protected]'); (сделал тестовый, в нем никакой авторизации не происходит. Чисто пытался сохранить сессию)
    Route::post('/login', 'auth\[email protected]')->name('login');
});


login method in LoginController
public function login(Request $request) {
        if(Auth::attempt(['email'=>$request->email, 'password'=>$request->password])) {
            if($request->session()->start()) {
                $request->session()->put(['user_id' => 32324234234]);
                $request->session()->save();
            }
        } else {
            return 'Вы ввели неверный логин или пароль';
        }
    }


The most interesting thing is that the session is written to a file along the path storage\framework\sessions.
And it doesn't matter if my route is in the route group or not. file is specified in session.php or database

What am I doing wrong?)

Thanks in advance for your reply

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Northern Lights, 2020-02-11
@Apostol63

Try it:php artisan config:cache

It is necessary to save the session to the database after user authentication.
why the base?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question