Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda