2
votes

I'm trying to Put Some values into session after Successful Login in Laravel for e.g name, img, username, last_login etc. I tried using Auth::user() which fetches every details from users detail when echoed.

How can i immediately put some other parameter from different table when user login successfully

My LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username()
    {
        return 'username';
    }

    public function credentials(Request $request)
    {
        return [
            'username' => $request->username,
            'password' => $request->password,
        ];
    }

    protected function authenticated(Request $request, $user)
    {
        if ( !$user->activated ) {
            auth()->logout();

            return back()->withErrors(['email' => 'Your account is not activated yet, please verify your Account.']);
        }

        return redirect()->intended($this->redirectPath());
    }
}
1
What version of Laravel 5 are you using?Rwd
i'm using 5.4 versionSagar Singh
Maybe you can use Event Subscribers to set the correct session values. Check out this link: laravel.com/docs/5.4/events#event-subscribersJirennor
What is the information from another table that you want to add to the session?Rwd
count his post, total friends, total time etc from other tableSagar Singh

1 Answers

1
votes

Once you attempt login i.e after user gets authenticated, try this

$request->session()->put('auth_user_session_object', Auth::user());