以「Laravel 教程 」為基礎評論 AT 多人通知多人功能。
前言
這是你想要的 AT 某人的功能
由於自己也要做個評論 AT 某人高亮功能,正好看到帖子就有。但是讀了下原始碼發現老哥好像只通知了一人。所以自己根據老哥的部分原始碼加以改動。以下是程式碼。
php artisan make:job ReplySomePeople
<?php
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));
}