需求:使用者發貼,粉絲收到通知。
瞭解了一下生成器,就用一下,雖然理解的還不是很深刻。
<?php
namespace App\Jobs;
use App\Models\User;
use App\Models\UserFollow;
use App\Notifications\NewThread;
class NewThreadNotification extends Job {
/** @var mixed */
private $thread;
private $actor;
public function __construct($thread,$actor) {
$this->thread = $thread;
$this->actor = $actor;
}
public function handle()
{
app('log')->info('----粉絲通知--這裡是傳送通知佇列-----');
//獲取這個使用者的所有粉絲
$follow = UserFollow::query()
->where('to_user_id',$this->thread->user_id)
->with('fromUser')
->get();
$users = $this->getFollowUser($follow);
foreach ($users as $v){
//傳送微信通知
$v->fromUser->notify(new NewThread($this->actor,$this->thread));
}
app('log')->info('----粉絲通知--傳送完成-----');
$this->delete();
}
public function getFollowUser($follow)
{
foreach ($follow as $v){
yield $v;
}
}
}
還在考慮,要不要分組,目前粉絲量頂天幾萬。以上是初體驗。。。
分片處理:
laravel cursor()處理:
cursor()原始碼也看了一下,我猜laravel裡面的懶載入應該是這個原理;
本作品採用《CC 協議》,轉載必須註明作者和本文連結