[英]Get total number of post views of all posts from a user in laravel 9
I am trying to get the total number of views of all posts from each user and display it to the user dashboard.我正在尝试获取每个用户的所有帖子的总查看次数并将其显示到用户仪表板。
I added a "reads" column to my article migration.我在文章迁移中添加了“阅读”列。
public function up()
{
Schema::table('articles', function (Blueprint $table) {
$table->bigInteger('reads')->default(0)->index();
});
}
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'));
}
In my ArticlesController I display the number of views on an article在我的 ArticlesController 中,我显示文章的浏览次数
public function index(User $user, Article $article)
{
return view('dashboard', compact('user', 'article'));
}
How do I display the total number of all article views on the user dashboard?如何在用户仪表板上显示所有文章查看的总数? My home controller:我的家庭控制器:
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.