在多对多中获取不是 user()->auth() 的收件人

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

我有一个消息系统,其中一个对话有很多 Messages.A 对话也有很多收件人(用户)。我想要做的是向所有用户发送通知,除了 sending/replying 到对话消息的经过身份验证的用户。

Tables(对于有帮助的人)

对话Table

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

留言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');

控制器方法

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);
}

关系

对话模型(有问题的代码)

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

我想知道如何获得 recipients() 而不是 auth()->user()->id,但似乎没有办法像 whereNotInPivot() 在维护集合的同时,我需要通知所有用户。

控制器

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);
}