Level 1
Guess I've found the right answer. When auth operations are run by a cron job or queue, auth()->user() returns null. Because these operations run in a CLI environment, not a web environment.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I wanted to trigger an event at certain intervals with Schedule
class CheckInactiveUsers implements ShouldQueue
{
use Queueable;
/**
* Create a new job instance.
*/
public function __construct()
{
//
}
/**
* Execute the job.
*/
public function handle(): void
{
$inactiveUsers = \App\Models\User::where('last_login', '<', now()->subDays(30))->get();
event(new \App\Events\InactiveUsersFound($inactiveUsers));
}
}
The code below doesn't work
public function broadcastOn(): array
{
return [
new PrivateChannel("inactive-users." . auth()->id())
];
}
But if I change it to public, it works
public function broadcastOn(): array
{
return [
new Channel("inactive-users") // Also changed the channel declaration in channels.php
];
}
What do you think is the problem?
Please or to participate in this conversation.