I have a list of all messages like this which contains the user id who sent it
$messages= Chat::all();
And a list of blocked users like this with the id of the user who was blocked
$blockedUsers=BlockUser::where('user_id', $user->id)->get();
My question is How to remove a message if the user id of the message exist in the blocked list?
CodePudding user response:
The filter messages, blocked users
public function getMessages(): JsonResponsive
{
$messages = Chat::where('user_id', '!=', $blockedUserId)->get();
return response()->json($messages)
}
CodePudding user response:
To delete all messages of a blocked user you can do it in the following way.
if ($blockedUsers = BlockUser::pluck('id')) {
$messages = Chat::whereNotIn('user_id', $blockedUsers)->get();
}
It should be noted that there are many ways to do this, directly with 1 single query doing Subqueries, but this will be enough to solve your problem and you will understand it much better, the other ways require a deeper knowledge of the framework.