最近发表的话题
最近发表的评论
  • 从这里开始学习 Go ——《Go 入门指南》(翻译完善中... ) at 3周前

    @Summer 感谢

  • 从这里开始学习 Go ——《Go 入门指南》(翻译完善中... ) at 3周前

    @Summer 没归类到社区文档模块下?

  • 针对本站招聘区制作了 RSS (同时支持标题关键词过滤后邮件推送) at 3周前

    弄了一个翻译模块的 rss

    https://feed43.com/6084862663437617.xml

  • 从这里开始学习 Go ——《Go 入门指南》(翻译完善中... ) at 3周前

    没迁移到新的平台

  • mailable was not queued? at 3周前
    class PleaseConfirmYourEmail extends Mailable implements ShouldQueue
    {
    
    }

    同样的报错
    implements ShouldQueue 就可以了,之前没有 implements

  • 针对 `calculateActiveUsers` 的一些优化 at 1个月前

    躺床上想了想,还能再优化,一条 SQL 搞定。

    
    trait ActiveUserCollectionHelper
    {
        private function calculateActiveUsers()
        {
            $active_users = $this->calculateScore();
    
            if ($active_users->isEmpty()) {
                return collect([]);
            }
    
            // 按照得分倒序排序,只获取我们想要的数量
            return $active_users->sortByDesc('score')->slice(0, $this->user_number);
        }
    
        private function calculateScore()
        {
            // 从用户表里取出用户限定时间范围($pass_days)内,发表过的话题数量和回复数量
            $active_users = User::where(function ($query) {
                $query->wherehas('topics', function ($query) {
                    $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));
                })
                    ->orWhereHas('replies', function ($query) {
                        $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));
                    });
            })
                ->withCount(['topics' => function ($query) {
                    $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));
                }])->withCount(['replies' => function ($query) {
                    $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));
                }])
                ->latest('topics_count')
                ->latest('replies_count')
                ->get();
    
            if ($active_users->isNotEmpty()) {
                $active_users->map(function ($user) {
                    $score = $user->topic_count * $this->topic_weight + $user->replies_count * $this->reply_weight;
                    $user->score = $score;
                    return $user;
                });
            }
    
            return $active_users;
        }
    }

    SQL 查询如下: (测试方便取的是30天内的活跃用户)

    SELECT `users`.*,
           (SELECT Count(*)
            FROM   `topics`
            WHERE  `users`.`id` = `topics`.`user_id`
               AND `created_at` >= '2018-12-13 12:04:29') AS `topics_count`,
           (SELECT Count(*)
            FROM   `replies`
            WHERE  `users`.`id` = `replies`.`user_id`
               AND `created_at` >= '2018-12-13 12:04:29') AS `replies_count`
    FROM   `users`
    WHERE  ( EXISTS (SELECT *
                     FROM   `topics`
                     WHERE  `users`.`id` = `topics`.`user_id`
                        AND `created_at` >= '2018-12-13 12:04:29')
          OR EXISTS (SELECT *
                     FROM   `replies`
                     WHERE  `users`.`id` = `replies`.`user_id`
                        AND `created_at` >= '2018-12-13 12:04:29') )
    ORDER  BY `topics_count` DESC,
              `replies_count` DESC

    还有个问题,你会发现我们不断重复写

     $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));

    这可以抽离成 model 的一个 scope 方法

    例如
    Models/Model.php

    
    use Carbon\Carbon;
    class Model extends EloquentModel
    {
    .
    .
    .
        public function scopeCreatedGreatThanPassDays($query, $pass_days)
        {
            return $query->where('created_at', '>=', Carbon::now()->subDays($pass_days));
        }

    那么

    $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));

    这一句可以改成

    $query->createdGreatThanPassDays($this->pass_days);
  • 针对 `calculateActiveUsers` 的一些优化 at 1个月前

    根据上面的代码用集合 Collection 的方式实现了一个版本,其他一样的代码就不贴了

    trait ActiveUserCollectionHelper
    {
        public function calculateAndCacheActiveUsers()
        {
            // 取得活跃用户列表
            $active_users = $this->calculateActiveUsers();
            // 并加以缓存
            $this->cacheActiveUsers($active_users);
        }
    
        private function calculateActiveUsers()
        {
            $this->calculateTopicScore();
            $this->calculateReplyScore();
    
            if (!$this->users) {
                return collect([]);
            }
    
            // 按照得分倒序排序,只获取我们想要的数量
            return collect($this->users)->sortByDesc(function ($user) {
                return $user['score'];
            })->slice(0, $this->user_number)->map(function ($item) {
                return (object)$item; //将数组转换成对象,前端模版无需修改
            });
        }
    
        private function calculateTopicScore()
        {
            // 从话题数据表里取出限定时间范围($pass_days)内,有发表过话题的用户
            // 并且同时取出用户此段时间内发布话题的数量
            $topic_users = User::withCount(['topics' => function ($query) {
                $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));
            }])
                ->orderBy('topics_count', 'desc')->get();
    
            if ($topic_users->isNotEmpty()) {
                $topic_users->map(function ($topic_user) {
                    $topic_score = $topic_user->topic_count * $this->topic_weight;
                    if (isset($this->users[$topic_user->id])) {
                        $this->users[$topic_user->id]['score'] += $topic_score;
                    } else {
                        $this->users[$topic_user->id]['score'] = $topic_score;
                    }
                    $this->users[$topic_user->id]['avatar'] = $topic_user->avatar;
                    $this->users[$topic_user->id]['name'] = $topic_user->name;
                    $this->users[$topic_user->id]['id'] = $topic_user->id;
                    return $this->users;
                });
            }
        }
    
        private function calculateReplyScore()
        {
            // 从回复数据表里取出限定时间范围($pass_days)内,有发表过回复的用户
            // 并且同时取出用户此段时间内发布回复的数量
            $replies_users = User::withCount(['replies' => function ($query) {
                $query->where('created_at', '>=', Carbon::now()->subDays($this->pass_days));
            }])
                ->orderBy('replies_count', 'desc')->get();
    
            if ($replies_users->isNotEmpty()) {
                $replies_users->map(function ($reply_user) {
                    $reply_score = $reply_user->replies_count * $this->reply_weight;
                    if (isset($this->users[$reply_user->id])) {
                        $this->users[$reply_user->id]['score'] += $reply_score;
                    } else {
                        $this->users[$reply_user->id]['score'] = $reply_score;
                    }
                    $this->users[$reply_user->id]['avatar'] = $reply_user->avatar;
                    $this->users[$reply_user->id]['name'] = $reply_user->name;
                    $this->users[$reply_user->id]['id'] = $reply_user->id;
                    return $this->users;
                });
            }
        }
    }

    SQL 如下:两条 SQL 解决,然后使用集合的方法去排序,只获取我们想要的数量
    file

  • 资源删除时没有更新缓存? at 1个月前
    class LinkObserver
    {
        // 在保存时清空 cache_key 对应的缓存
        public function saved(Link $link)
        {
            Cache::forget($link->cache_key);
        }
    
        public function deleted(Link $link)
        {
            Cache::forget($link->cache_key);
        }
    }

    这样吧

  • The given role or permission should use guard `` instead of `Web`? at 1个月前

    Running:

    php artisan cache:forget spatie.permission.cache
    php artisan cache:clear

  • 新手怎么解决 NPM run dev 报错的问题,综合回答一下,希望能帮到后来人 at 1个月前

    @xiayin 还是这个有效

  • 解决翻译结果是拼音问题! at 1个月前

    @fresh1204 宿主机的地址

  • [扩展推荐] Larastan —— Laravel 静态代码分析工具 at 2个月前

    安装的时候如果出现提示 Your requirements could not be resolved to an installable set of packages.
    改用以下的命令安装
    先执行 composer update
    再执行 composer require nunomaduro/larastan --update-with-all-dependencies

  • [已完成] 翻译《PHP 设计模式》 at 10个月前

    @bestcyt ireader 支持 epub 吧,

  • [已完成] 翻译《PHP 设计模式》 at 10个月前

    想要离线版可以放kindle里看