Today we’ll let admins use natural phrases like:
- “customer updates over RM500 last month”
- “deletions by admin this week”
- “changes to records yesterday”
GPT will turn those into real-time Laravel where()
queries — filtering audit logs automatically and returning results.
🧠 Step 1: Extend the function definition for filters
Update your GPT function list in AuditQueryAssistant.php
:
$functions = [
[
'name' => 'filter_audit_logs',
'description' => 'Filter Laravel audit logs using model, user, date, and keyword',
'parameters' => [
'type' => 'object',
'properties' => [
'model' => ['type' => 'string', 'description' => 'Model affected, e.g. Customer, Vehicle'],
'user' => ['type' => 'string', 'description' => 'User who did the action (optional)'],
'action' => ['type' => 'string', 'description' => 'Action like created, updated, deleted'],
'date' => ['type' => 'string', 'description' => 'Date like yesterday, last 30 days'],
'keyword' => ['type' => 'string', 'description' => 'Any other keyword or field reference'],
],
'required' => ['model', 'action'],
],
]
];
⚙️ Step 2: Add filterLogs
method
Still in AuditQueryAssistant.php
:
private static function runQuery(array $args): string
{
$query = Activity::with('causer');
if (!empty($args['model'])) {
$query->where('subject_type', 'like', '%' . $args['model'] . '%');
}
if (!empty($args['action'])) {
$query->where('description', 'like', '%' . $args['action'] . '%');
}
if (!empty($args['user'])) {
$query->whereHas('causer', function ($q) use ($args) {
$q->where('name', 'like', '%' . $args['user'] . '%');
});
}
if (!empty($args['date'])) {
if (str_contains($args['date'], 'yesterday')) {
$query->whereDate('created_at', \Carbon\Carbon::yesterday());
} elseif (str_contains($args['date'], 'last 30')) {
$query->where('created_at', '>=', now()->subDays(30));
} elseif (str_contains($args['date'], 'this week')) {
$query->whereBetween('created_at', [now()->startOfWeek(), now()]);
}
}
if (!empty($args['keyword'])) {
$query->where('properties', 'like', '%' . $args['keyword'] . '%');
}
$logs = $query->limit(30)->latest()->get();
if ($logs->isEmpty()) {
return 'No matching results found.';
}
return \App\Helpers\AuditSummaryHelper::translateToPlainEnglish($logs);
}
📡 Step 3: Reuse the existing /ask
route
No changes to routes needed — just let GPT interpret the new structure automatically.
💬 Step 4: Try some natural queries
In your existing chat box (index.blade.php
):
<input name="question" class="border px-2 py-1 w-96" placeholder="e.g. customer updates over RM500 last month">
✅ Example Inputs & GPT Responses
Input:
“Show me updates to records by IT last 7 days”
Output:
The IT department updated 3 records from 3–10 June. Admin Tan modified renewal dates and prices above RM500.
✅ Tomorrow (Day 10), we’ll complete the series by logging GPT activity, adding abuse protection, and preparing your audit tools for production.