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

akinchilipepper's avatar

Laravel private channels doesn't work when scheduling

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?

0 likes
3 replies
akinchilipepper's avatar

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.

Snapey's avatar

You could find the id of the user you want to broadcast to

$user = User::where('email' , '[email protected]')->first();

return  [
    new PrivateChannel("inactive-users." . $user->id)
];

Please or to participate in this conversation.