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

无ActivityLog模型时,如何将activity_log表数据渲染到Blade模板

如何将Spatie活动日志展示到Blade表格视图

我首次使用Spatie包,已经成功集成用于记录活动日志的Spatie包,并在用户登录成功时将数据写入activity_log表。但尚未创建ActivityLog模型,需要了解如何将activity_log表中的数据展示到blade.php文件的表格视图中。

我的迁移文件:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateActivityLogTable extends Migration
{
    public function up()
    {
        Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('log_name')->nullable();
            $table->text('description');
            $table->nullableMorphs('subject', 'subject');
            $table->nullableMorphs('causer', 'causer');
            $table->json('properties')->nullable();
            $table->timestamps();
            $table->index('log_name');
        });
    }

    public function down()
    {
        Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
    }
}

我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;


class UserLogsController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $users = User::all();
        return view('user_logs', compact('users'));
    }
}

解决步骤

1. 创建ActivityLog模型

在app/Models目录下新建ActivityLog.php文件,内容如下:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ActivityLog extends Model
{
    // 指定对应的数据表
    protected $table = 'activity_log';

    // 允许批量赋值的字段
    protected $fillable = [
        'log_name', 'description', 'subject_type', 'subject_id',
        'causer_type', 'causer_id', 'properties'
    ];

    // 关联操作用户(causer对应User模型)
    public function causer()
    {
        return $this->morphTo();
    }

    // 关联主题模型(按需启用)
    public function subject()
    {
        return $this->morphTo();
    }
}

2. 修改控制器获取日志数据

更新UserLogsController的index方法,获取活动日志并关联操作用户信息:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\ActivityLog;


class UserLogsController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        // 按时间倒序获取日志,关联操作用户
        $activityLogs = ActivityLog::with('causer')->latest()->get();
        return view('user_logs', compact('activityLogs'));
    }
}

3. 编写Blade表格视图

在resources/views目录下的user_logs.blade.php中添加表格代码:

<!DOCTYPE html>
<html>
<head>
    <title>用户活动日志</title>
    <!-- 引入Bootstrap美化表格 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h2>用户活动日志</h2>
        <table class="table table-striped mt-3">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>日志名称</th>
                    <th>操作描述</th>
                    <th>操作用户</th>
                    <th>操作时间</th>
                </tr>
            </thead>
            <tbody>
                @foreach($activityLogs as $log)
                <tr>
                    <td>{{ $log->id }}</td>
                    <td>{{ $log->log_name ?? '无' }}</td>
                    <td>{{ $log->description }}</td>
                    <td>{{ $log->causer ? $log->causer->name : '未知用户' }}</td>
                    <td>{{ $log->created_at->format('Y-m-d H:i:s') }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</body>
</html>

4. 可选优化

  • 分页:将get()替换为paginate(10),然后在视图底部添加{{ $activityLogs->links() }}实现分页
  • 展示额外属性:如果properties字段存储了自定义数据,可通过$log->properties['key']访问(需先判断字段存在)

内容的提问来源于stack exchange,提问作者Big Brother

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 19:22:04