我收到某人发布新帖子时需要发送的通知。但是我需要将这些通知仅发送给该主题中的人。
我制作了一个通知表,并设法将通知显示给用户,但我需要对其进行过滤。
这些是我要使用的相关表:
这是帖子:
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('subject_id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->text('text');
$table->foreign('subject_id')->references('id')->on('subjects')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
主题:
$table->bigIncrements('id');
$table->timestamps();
$table->string('name');
$table->string('text');
$table->unsignedInteger('year');
以及标准用户和通知表。因此,我基本上需要将通知发送给特定主题的用户。因此,如果发布主题为id = 1的帖子,则会将通知发送给具有subject_id = 1的用户。对于这种关系,我有此表:
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('subject_id');
$table->unsignedBigInteger('user_id');
$table->foreign('subject_id')->references('id')->on('subjects')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
因此,这将连接我的用户ID和主题ID。
我尝试过这样的事情:
foreach ($users as $user) {
if($user->subjects()->id==$subject_id)
$user->notify(new Notify($subject_id, $obavjestenje));
}
但这不起作用。
答案 0 :(得分:0)
您可以像这样获得所有具有主题的用户,并排除发布者用户
times
A B C passed_time
0 23 a 2019-07-05 12:22:22 2019-07-20 12:22:22
1 22 a 2019-07-10 12:22:22 2019-07-25 12:22:22
2 25 a 2019-07-15 12:22:22 2019-07-35 12:22:22
3 23 b 2019-07-20 12:22:22 2019-07-20 12:22:22
4 22 b 2019-07-25 12:22:22 2019-07-25 12:22:22
5 23 c 2019-07-30 12:22:22 2019-07-20 12:22:22
6 25 b 2019-07-35 12:22:22 2019-07-35 12:22:22
7 25 c 2019-07-40 12:22:22 2019-07-35 12:22:22
8 22 a 2019-07-45 12:22:22 2019-07-25 12:22:22
是创建帖子的用户的ID
然后您可以发送通知
// get all users that have matching subject
$users = User::whereHas('subjects', function($query) use ($subject_id) {
$query->where('id', '=', $subject_id);
})
// exclude user that created the post
->where('user_id', '<>', $post_user_id)
->get();