「拓展」实现评论 @ 多人通知功能 
                                                    
                        
                    
                    
  
                    
                    以「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));
    }本作品采用《CC 协议》,转载必须注明作者和本文链接
 
           peryiqiao 的个人博客
 peryiqiao 的个人博客
         
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
                     
                     
             
             
             
         
           
           关于 LearnKu
                关于 LearnKu
               
                     
                     
                     粤公网安备 44030502004330号
 粤公网安备 44030502004330号 
 
推荐文章: