Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!