You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9:如何在后台统计未登录用户访问量及总访问量?

Laravel 9 后台统计数据实现方案(用户总数、访问量统计)

一、用户总数统计(零成本实现)

直接通过Eloquent模型查询即可,在你的后台控制器方法中添加:

// 假设用户模型路径为 App\Models\User
$totalUsers = \App\Models\User::count();

将变量传递到后台视图后,在Blade模板中直接渲染:

<p>用户总数:{{ $totalUsers }}</p>

二、访问量统计(区分登录/未登录用户)

这部分需要先记录访问日志,再通过日志数据统计,步骤如下:

1. 创建访问日志表与模型

生成迁移文件:

php artisan make:migration create_visitor_logs_table

打开迁移文件,修改up方法定义表结构:

public function up()
{
    Schema::create('visitor_logs', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id')->nullable(); // 登录用户ID,未登录则为null
        $table->string('ip_address'); // 访问IP
        $table->string('url'); // 访问的完整URL
        $table->timestamps();
    });
}

执行迁移生成表:

php artisan migrate

生成对应模型:

php artisan make:model VisitorLog

在模型中设置可填充字段:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class VisitorLog extends Model
{
    use HasFactory;

    protected $fillable = ['user_id', 'ip_address', 'url'];
}

2. 编写访问记录中间件

生成中间件:

php artisan make:middleware RecordVisitor

打开app/Http/Middleware/RecordVisitor.php,修改handle方法捕获请求并记录:

public function handle(Request $request, Closure $next)
{
    // 可选:排除后台路由,避免统计管理员自身操作
    if ($request->is('admin/*')) {
        return $next($request);
    }

    // 写入访问日志
    \App\Models\VisitorLog::create([
        'user_id' => auth()->check() ? auth()->id() : null,
        'ip_address' => $request->ip(),
        'url' => $request->fullUrl(),
    ]);

    return $next($request);
}

将中间件注册到web路由组(在app/Http/Kernel.php的$middlewareGroups中):

protected $middlewareGroups = [
    'web' => [
        // 保留原有中间件,添加下面一行
        \App\Http\Middleware\RecordVisitor::class,
    ],
];

3. 查询访问统计数据

在后台控制器中添加统计查询:

// 网站总访问量
$totalVisits = \App\Models\VisitorLog::count();

// 未登录用户访问量
$guestVisits = \App\Models\VisitorLog::whereNull('user_id')->count();

将$totalVisits和$guestVisits与$totalUsers一起传递到视图。

三、后台视图展示示例

在后台仪表盘视图(如resources/views/admin/dashboard.blade.php)中添加统计卡片:

<div style="display: flex; gap: 20px; margin: 20px 0;">
    <div style="padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); flex: 1; text-align: center;">
        <h3>用户总数</h3>
        <p style="font-size: 24px; font-weight: bold; color: #333; margin-top: 10px;">{{ $totalUsers }}</p>
    </div>
    <div style="padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); flex: 1; text-align: center;">
        <h3>网站总访问量</h3>
        <p style="font-size: 24px; font-weight: bold; color: #333; margin-top: 10px;">{{ $totalVisits }}</p>
    </div>
    <div style="padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); flex: 1; text-align: center;">
        <h3>未登录用户访问量</h3>
        <p style="font-size: 24px; font-weight: bold; color: #333; margin-top: 10px;">{{ $guestVisits }}</p>
    </div>
</div>

内容的提问来源于stack exchange,提问作者Aldo Nezha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 05:30:43