这是我第一次使用 Spatie 包。我已经成功地将 Spatie 包(一个活动日志包)集成到我的代码中,并在用户成功登录时将数据插入到我的 Activity_log 表中。不过,我需要弄清楚如何将 Activity_log 表数据库中的数据显示到当我没有 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'));
}
}
这是我数据库中的 Activity_log 表
如果您想将数据检索到具有表数据视图的刀片视图中,您可以使用以下命令:
<!-- user_logs.blade.php -->
<table>
<thead>
<tr>
<th>ID</th>
<th>Log Name</th>
<th>Description</th>
<!-- Add more table headers as needed -->
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->log_name }}</td>
<td>{{ $user->description }}</td>
<!-- Add more table cells based on your table structure -->
</tr>
@endforeach
</tbody>
</table>