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

kendrick's avatar

Request session() in Job (Exception: call to undefined method session())

Hey, I am trying to send a Mail, based on the sessions language value.

SendMailJob


public function handle()
    {
        $email = new HelloMail($this->user);
 
        if ($this->session()->has('lang') == 'en') {
            $language = 'en';
        } elseif ($this->session()->has('lang') == 'de') {
            $language = 'de';
        } else {
            $language = 'en';
        }

        Mail::to($this->user->email)->locale($language)->send($email);
    }

I am running into Call to undefined method HelloMail::session()

Any suggestions how to get the session()->has('lang') attribute?

Thank you

0 likes
5 replies
tykus's avatar

$this->session()?

Besides, there would be no session if the job was queued

kendrick's avatar

Should be $request->session() ?

How could I solve it?

Yorki's avatar
Yorki
Best Answer
Level 11

You should pass language while queueing job.

dispatch(new SendMailJob($request->session()->get('lang', 'en')));
public function __construct($lang)
{
    $this->lang = $lang;
}

public function handle()
{
    Mail::to($this->user->email)->locale($this->lang)->send(new HelloMail($this->user));
}
Yorki's avatar

@Jurrien You don't know what are you talking about. This is job method, not controller one. These are fired from consumers having no access to session.

Please or to participate in this conversation.