php通知多有人,「拓展」实现评论 @ 多人通知功能

以「Laravel 教程 」为基础评论 AT 多人通知多人功能。

前言

这是你想要的 AT 某人的功能

由于自己也要做个评论 AT 某人高亮功能,正好看到帖子就有。但是读了下源码发现老哥好像只通知了一人。所以自己根据老哥的部分源码加以改动。以下是代码。

php artisan make:job ReplySomePeople

namespace App\Jobs;

use App\Models\Reply;

use App\Models\User;

use App\Notifications\ArticleReplied;

use Illuminate\Bus\Queueable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Support\Facades\Notification;

class ReplySomePeople implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $reply;

/**

* ReplySomePeople constructor.

*

* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)param \App\Models\Reply $reply

*/

public function __construct(Reply $reply)

{

$this->reply = $reply;

}

/**

* Execute the job.

*

* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)return void

*/

public function handle()

{

$content = $this->reply->content;

$contentFormat = matchAt($content);

$this->reply->content = $contentFormat['body'];

$this->reply->update([

'content' => $this->reply->content

]);

if (isset($contentFormat['reply_user_ids'])) {

$reply_user_ids = array_unique($contentFormat['reply_user_ids']);//去重

$users = User::whereIn('id', $reply_user_ids)->get();//用户列表

User::whereIn('id', $reply_user_ids)->increment('notification_count');//评论+1

Notification::send($users, new ArticleReplied($this->reply));

}

}

}

matchAt 方法「bootstrap/helpers.php」

/**

* 正则替换 [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278) 用户

* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)param $body

*

* [[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)return array

*/

function matchAt($body){

$userModel = new \App\Models\User();

preg_match_all("/\[[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)([^\#|\s]+)\s/", $body, $matched_name);

if (!empty($matched_name[1])) {

$reply_user_ids = [];

foreach($matched_name[1] as $key => $name) {

$user_id = $userModel->where('name', 'like binary', $name)->value('id');

if(!$user_id) {

continue;

}

$reply_user_ids[$key] = $user_id;

$body = str_replace('[[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)'.$name,'[[[[[@](https://learnku.com/users/10240)](https://learnku.com/users/8278)](https://learnku.com/users/10240)](https://learnku.com/users/8278)'.$name.']('.config('app.url').'/users/'.$reply_user_ids[$key].')',$body);

}

return ['reply_user_ids' => $reply_user_ids, 'body' => $body];

} else {

return ['body' => $body];

}

}

最后在 「ReplyObserver」修改 「created 」方法

use App\Jobs\ReplySomePeople;

public function created(Reply $reply)

{

//文章评论数

$article = $reply->article;

$article->last_reply_user_id = Auth::id();//最后评论

$article->increment('reply_count', 1);//评论数+1

dispatch(new ReplySomePeople($reply));

// $article->user->notify(new ArticleReplied($reply));

}

本作品采用《CC 协议》,转载必须注明作者和本文链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值