42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ActivityLog;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ActivityLogController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = ActivityLog::with(['user', 'loggable'])->latest();
|
|
|
|
// 1. Filter by Action
|
|
if ($request->filled('action_type')) {
|
|
$query->where('action', $request->action_type);
|
|
}
|
|
|
|
// 2. Filter by Date Range
|
|
if ($request->filled('date_from')) {
|
|
$query->whereDate('created_at', '>=', $request->date_from);
|
|
}
|
|
if ($request->filled('date_to')) {
|
|
$query->whereDate('created_at', '<=', $request->date_to);
|
|
}
|
|
|
|
// 3. Search by User or Entity Type
|
|
if ($request->filled('search')) {
|
|
$searchTerm = $request->search;
|
|
$query->where(function ($q) use ($searchTerm) {
|
|
$q->whereHas('user', function ($userQuery) use ($searchTerm) {
|
|
$userQuery->where('name', 'like', '%' . $searchTerm . '%');
|
|
})
|
|
->orWhere('loggable_type', 'like', '%' . $searchTerm . '%');
|
|
});
|
|
}
|
|
|
|
$logs = $query->paginate(20)->withQueryString();
|
|
|
|
return view('activity-logs.index', compact('logs'));
|
|
}
|
|
} |