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

salmankhan2482's avatar

how to get session variable inside cron job laravel

i m using laravel and have stored the value in the session key named as 'flag'

$flag = $_GET['flag']; //getting the values from request

session()->put('flag', $flag); //storing the value here in session key

but when i try to access or get it inside the cronjob file, nothing happens

if(session('flag')=='bill_full_payment_check'){

$invoice->bill_full_payment_check = 1;

$invoice->save();

session()->forget('flag');

    }
}
0 likes
11 replies
CorvS's avatar
CorvS
Best Answer
Level 27

@salmankhan2482 Jobs are not bound to a request, which means there is no session available. Store the flag inside the database.

2 likes
CorvS's avatar

@salmankhan2482 It depends, what exactly is the flag supposed to do? Is it related to that particular invoice or job? If it's related to the job you can pass that flag inside the constructor of your job.

mane_olawale's avatar

Is this a Queue Job, or a scheduled Task. in your post you said Cron Job and people are replying with job,

You need to tell us what you are trying to pull off

1 like
salmankhan2482's avatar

it actually stores the value from the check box input. and i am trying to solve it by inserting it in the database and then retrieve it from the database in cron job.

mane_olawale's avatar

Cron jobs are from the cli, Their processes do not have session, there are other alternative You can persist the "bill_full_payment_check" to the database in your invoice table

or you can explain what you are really trying to do

1 like
martinbean's avatar

@salmankhan2482 Sessions are used in web requests. A queued job is executed on the command line; there is no session. You need to pass any information your job needs to run as a parameter.

It also doesn’t make sense to use sessions in a queued job. The user can log out of your website in the browser before the queue’s picked up and processed the job.

1 like
salmankhan2482's avatar

thank you sir for the reply and i did forget the session when the job is done. hence the session cannot be accessed in cron job so i followed the method to insert it in the database and then access it from the database.

Please or to participate in this conversation.