Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

samer's avatar
Level 1

How to add Session to the Laravel Auth ?

I would like to create new session after user login

0 likes
6 replies
usama.ashraf's avatar

@samer ofcourse the user session is automatically created when you login. You can access that any where in your code like: auth()->user()

If you want to start another session when the user logs in, you can do that by overriding/editing either of the following in the Auth controller:

  1. edit create method to start a session.
  2. override the login method in the auth controller that comes from the AuthenticatesAndRegistersUsers trait.
1 like
samer's avatar
Level 1

i'm using the laravel auth and after login i would like to use the username and all information related to the logged in user

samer's avatar
Level 1

thnx , can you tell me the difference between cookie and file in config/session.php ?

adwairi's avatar

Hi, I have two tables, table User and table Accounts (User hasMany Accounts). After login, i need to push the related accounts to the Auth session or to another session. Where I have to put my code? and how to do that?

Cronix's avatar

It sounds like you want to always load the Accounts relationship (assuming you have a relationship defined)?

If so, in your user model, you can just add the public $with = ['accounts'];

And then any time the user is loaded, so will their accounts, which would be accessed via Auth::user()->accounts; (or anywhere else that has the user object)

class User extends Authenticatable
{
    //whenever user is retrieved, load their accounts
    public $with = ['accounts'];  

    public function accounts()
    {
        return $this->hasMany('App\Accounts');
    }
}
1 like

Please or to participate in this conversation.