首页 > 解决方案 > 控制器中的序列码问题

问题描述

$trendtag仅代码有效。但是,如果我想加载帖子,它不会得到帖子。

如果我运行dd($tagIdArray),我会得到id标签的 's。但是下面的代码没有任何功能。如果我不使用 $trendtags,或者只是这样,它可以工作:

$trendtags = Taggable::take(3)
            ->with('tag')
            ->get();

但是我怎样才能找到过去 48 小时内最常用的标签?

还有其他方法吗?

$trendtags = Taggable::whereDate('created_at', '>=', now()->subHours(48))
            ->groupBy('tag_id')
            ->orderByRaw('count(tag_id) DESC')
            ->take(3)
            ->with('tag')
            ->get();

        $tagIdArray = $trendtags->pluck('id')->all();

    $article = Article::with('comments', 'tags')
      ->whereIn('privacy', [1, 2])
      ->where('status', 1)
      ->whereHas('tags', function($query) use ($tagIdArray) {
        $query->whereIn('id', $tagIdArray);
      }, '>=', count($tagIdArray))
      ->latest()
      ->paginate(15);

标签: laravelcontroller

解决方案


推荐阅读