Why don't you just broadcast to the user id instead of the post id?
Broadcast::channel('Users.{userId}', function (User $user, int $userId) {
return $user->friends()->where('friend_id', $userId)->exists();
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Within User.php I have a friends() relationship:
public function friends(){
return $this->belongsToMany(User::class, 'friends', 'user_id', 'friend_id')->withTimestamps();
}
Now, whenever a friend makes a Post.php, I would like to broadcast this Post only to friends
Currently I am checking on a Channel which has {postId} in it, and it don't works. It returns Posts.undefined, as I am listening to the Channel as a logged-in user, but a postId is logically not available.
Broadcast::channel('Posts.{postId}', function (User $user, int $postId) {
$post = Post::findOrFail($postId);
return $user->friends()->where('friend_id', $post->user_id)->exists();
});
Argument 2 passed to App\Providers\BroadcastServiceProvider::{closure}() must be of the type int, string given, called in App\Providers\BroadcastServiceProvider->{closure}(Object(App\Models\User), 'undefined')
Is there another way, to broadcast only friend related Posts?
Would it be possible if I check on userId within the Channel, and then check if the userId (authenticated user), counts a friend which made that Post?
Please or to participate in this conversation.