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

vincent15000's avatar

Retrieve session data from model

Hello,

I tried to retrieve a session data from the model.


public function getCurrentCompanyId()
{
    return session('company_id');
}

But it always returns null.

Whereas if I retieve the session data from a trait or any scope, it returns the right value stored in the session.

Why ?

Can you help me to understand ?

Thanks.

Vincent

0 likes
9 replies
vincent15000's avatar

Is it because a model doesn't depend on any session ? That would be logical.

tykus's avatar

@vincent15000 Generally, I would suggest that any Request-based concerns such as the Session are separated from the Model; as you will llikely want to use the Model in non-Request contexts. The hint in the application structure is the separate Http directory...

What were you trying to achieve by getting the current_company_id from the Model?

1 like
vincent15000's avatar

@tykus Well ... it's an app where a user (a trainer) can work for different training companies which will use the app.

So when a trainer connects to the app, if he is attached to several training companies, he has to choose for which one he wants to open the app, so that he will access to the datas of this specific company.

I thought it ws a good idea to store the selected company to the session.

And as it concerns a user, I thought it was a good idea to have a method in the user model to retrieve the value like this : auth()->user()->current_company_id.

But now I understand it's not the best way to create a method in the user model because it concerns the connected user and not the other users.

I need to use the current company id in many situations : policies, controllers, model scopes, ... to check if the user can do an action for this company, to retrieve only the courses from this company, ...

What would you suggest me to access easily to the current company id ? Or any other way than a session data ?

tykus's avatar
tykus
Best Answer
Level 104

@vincent15000 you could do this in a custom Middleware - it could be as simple as

public function handle($request, $next)
{
    if (auth()->check()) {
        auth()->user()->current_company_id = $request->session('company_id');
    }

    return $next($request);
}

You could initialize a public property on the User model for completeness:

public ?int $current_company_id = null;

Now, you can make this custom middleware global, or a route middleware; up to you. Just ensure it is handled after the Session is started and the User is authenticated

1 like
tykus's avatar

@vincent15000 the property is expecting an int type, but we're initializing it with null; so we make the type nullable.

1 like
vincent15000's avatar

@tykus If I use a middleware, that means that each time I load a page, it will assign the company id to the authenticated user.

What is the advantage compared to not using any middleware and accessing the current company id from session('company_id') everywhere in the code ?

What would probably be better if to have a global function getCurrentCompany() or getCurrentCompanyId() that could be accessed from anywhere in the code. But I don't know how to do that.

Please or to participate in this conversation.