Search code examples
laravelmany-to-manylaravel-6

Get recipients where not user()->auth() in many to many


I have a message system where a Conversation has many Messages.A conversation also has many recipients(Users). What I am trying to do is send a notification to all users except the authenticated user who is sending/replying to a conversations messages.

Tables (For those who it helps)

Conversation Table

$table->bigIncrements('id');
$table->string('hashed_id')->nullable();
$table->unsignedInteger('has_reply')->unsigned()->default(0);
$table->text('subject');
$table->timestamps();

Message Table

$table->bigIncrements('id');
$table->unsignedBigInteger('sender_id');
$table->unsignedBigInteger('conversation_id');
$table->text('body');
$table->timestamps();

$table->foreign('sender_id')->references('id')->on('users');
$table->foreign('conversation_id')->references('id')->on('conversations');

conversation_participant table

$table->integer('user_id')->unsigned();
$table->integer('conversation_id')->unsigned();
$table->tinyInteger('status')->default(1);
$table->tinyInteger('is_sender')->default(0);
$table->timestamps();

$table->foreign('conversation_id')->references('id')->on('conversations')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

Controller Method

InboxController

public function reply($hashedId, Request $request)
{
    $this->validate($request, [
        'body' => 'required',
    ]);

    $conversation = Conversation::where('hashed_id', $hashedId)->first();

    //Recipients that aren't auth
    $users = $conversation->recipients;

    $notifications = Notification::send($users, new MessageNotification());

    $message = $conversation->messages()->create([
        'sender_id' => auth()->user()->id,
        'body' => $request->body,
    ]);

    return new MessageResource($message);
}

Relationship

Conversation Model (Problematic code)

public function recipients()
{
  return $this->belongsToMany('App\User' ,'conversation_participants','conversation_id','user_id')->wherePivot('user_id',0);
}

What I'm wondering how to do is get recipients() that aren't the auth()->user()->id, but it doesn't seem like there is a way to do something like whereNotInPivot() while maintaining the collection I would need to notify all users.


Solution

  • Controller

    public function reply($hashedId, Request $request)
    {
        $this->validate($request, [
            'body' => 'required',
        ]);
    
        $conversation = Conversation::where('hashed_id', $hashedId)->first();
    
        //Recipients that aren't auth
        $users = $conversation->recipients()->where('users.id', '!=', auth()->id())->get();
    
        $notifications = Notification::send($users, new MessageNotification());
    
        $message = $conversation->messages()->create([
            'sender_id' => auth()->user()->id,
            'body' => $request->body,
        ]);
    
        return new MessageResource($message);
    }