php - 如何在laravel中添加帖子的查看次数
问题描述
我正在使用 laravel 执行博客应用程序。我想在用户查看特定博客文章时跟踪文章视图的计数,无论它是注册用户还是非注册用户,它都应该只增加一次。...并且还想根据查看次数显示浏览次数最多的博客。任何人都可以协助逻辑。
/ Table
Schema::create('posts', function(Blueprint $table)
{
$table->increments('id');
$table->text('body')
$table->integer('count');
...
$table->timestamps();
});
public function show($id)
{
$post = Post::find($id);
Post::update([
'count' => $post->count + 1
]);
return View::make('posts', compact('post'));
}
解决方案
你有两个要求:
- 增加每个视图的视图计数。
public function show($id)
{
$post = Post::find($id);
Post::update([
'count' => $post->count + 1
]);
return View::make('posts', compact('post'));
}
- 根据查看次数显示查看次数最多的博客
$post = Post::orderBy('count', 'DESC')->first();
- 另外,要根据查看次数显示查看次数最多的博客列表,
$post = Post::orderBy('count', 'DESC')->get();
推荐阅读
- java - 如何解决 java.net.SocketException:使用 CloseableHttpClient 发送 post 请求时连接重置?
- git - 如何使用 wget 在 unix 上安装 git?
- php - 为什么我收到为 foreach() 错误提供的无效参数?
- apl - APL 成语用零包围数组
- android - Daw 线穿过画布上的“细胞”
- javascript - 如何通过单击 jquery 将一个图像更改为另一个图像
- ios - 在多个文本字段上隐藏键盘
- javascript - 如何在javascript中覆盖数组数据
- sql - 将 Case 语句的结果从 SQL 获取回应用程序
- django - Django 表单和国际化 - 标签的语言错误