[英]How to get the total number of visits by the people who are not logged in laravel 9
所以我想在我的管理仪表板中查看一些统计信息,例如用户总数(这很容易做到)以及每个人(登录和用户)访问网站的总数以及访问总数(访问的人没有登录)? Laravel 中的新功能。
还没有尝试过喷射。
首先,您必须为您的仪表板制作一个表格来保存信息。 如果您仍然没有创建一个,请参阅文档。
然后,您可以使用此命令制作新的中间件php artisan make:middleware CountVisits
应用\Http\中间件\CountVisits.php:
public function handle(Request $request, Closure $next)
{
//Get the first row that contain the dashboard information
$dashboard = Dashboard::where('id', 1)->first();
//Get the current visits counter
$counter = $dashboard->visits_counter;
$updated_counter = $counter++;
//Update the field
$dashbord->update([
'visits_counter' => $updated_counter
]);
return $next($request);
}
这会将visits_coutner
的字段设置为给定的更新计数器。 该字段必须存在于您的迁移中。
应用\Http\Kernel.php:
protected $middleware = [
(...)
\App\Http\Middleware\CountVisits::class,
];
这将注册要全局应用于每个路由的中间件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.