Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
How to create a Laravel 5.4 Session in the Database

I've scoured the documentation and I've spent hours trying to figure this out, and this is really my last resort. If this doesn't work I may have to get a subscription to Laracasts. I'm following this guide and it doesn't seem to be working for me. https://laravel.com/docs/5.4/session

I'm hoping that someone can help me and tell me what I'm doing wrong. When I do my API request I get a success response and it returns the user from the database, but it doesn't create the session in the session table. Also, I don't get any errors in laravel.log.

I can create a user with no problems. I can match hashed passwords when retrieving the user.

api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('/users', 'UserApiController@login');

UserApiController.php

public function login()
{
    $input = Request::all();
    $user = new User();
    $response = ['success' => false];
    if (!$user->validate($input))
    {
        return response()->json($response, 412);
    }
    $user = User::where('Email', $input['Email'])->where('Password', User::makePassword($input['Password']))->first();

    Log::info($user); //returns the user object successfully

    if (!isset($user))
    {
        return response()->json($response, 401);
    }
    session('user', $input]);  //<--this is not working
    $response['success'] = true;
    return response()->json($response, 200);
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'm not sure if you wrote what you really want to achieve, but this is what I think - you are using api middleware group and you want to use sessions. But if you look at app/Http/Kernel.php you will see sessions are used by default only by web middleware group:

\Illuminate\Session\Middleware\StartSession::class,

so you can either add this middleware to api group or rethink what you really want to achieve because APIs are usually stateless so you normally don't use sessions for them because in each request you send authorization data (api_token in this case by default) so each time you can find out which user is making the request

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda