繁体   English   中英

在laravel 9中获取用户所有帖子的帖子浏览总数

[英]Get total number of post views of all posts from a user in laravel 9

我正在尝试获取每个用户的所有帖子的总查看次数并将其显示到用户仪表板。
我在文章迁移中添加了“阅读”列。

public function up()
    {
        Schema::table('articles', function (Blueprint $table) {
            $table->bigInteger('reads')->default(0)->index();
        });
    }

我的文章(帖子)模型中有一个名为 incrementReadCount() 的方法,它计算每篇文章的浏览量。
public function show(Article $article, User $user)
{
    $articles = $user->articles()->get();

    if(Cookie::get($article->id)!=''){
        Cookie::set('$article->id', '1', 60);
        $article_reads = $article->incrementReadCount();
    }
    return view('article.article', compact('article','user', 'articles', 'article_reads'));
}

在我的 ArticlesController 中,我显示文章的浏览次数

public function index(User $user, Article $article)
{

    return view('dashboard', compact('user', 'article'));
}

如何在用户仪表板上显示所有文章查看的总数? 我的家庭控制器:

 public function index(User $user, Article $article) { return view('dashboard', compact('user', 'article')); }

在您的 index 方法中,获取所有用户的文章并对“reads”列进行汇总:

$user->articles()->sum('reads');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM