neelkanthk/laravel-surveillance-ui
Install Dependencies
composer require neelkanthk/laravel-surveillance-ui
composer require neelkanthk/laravel-surveillance
Ensure laravel-surveillance is configured first (see its docs).
Publish Assets
php artisan vendor:publish --provider="Neelkanth\SurveillanceUI\SurveillanceUIServiceProvider" --tag="public"
php artisan vendor:publish --provider="Neelkanth\SurveillanceUI\SurveillanceUIServiceProvider" --tag="config"
This publishes the UI assets (CSS/JS) and config file to public/vendor/surveillance-ui/ and config/surveillance-ui.php.
Add Middleware
Register the UI middleware in app/Http/Kernel.php:
'web' => [
// ...
\Neelkanth\SurveillanceUI\Middleware\SurveillanceUI::class,
],
Or protect a specific route:
Route::middleware(['web', \Neelkanth\SurveillanceUI\Middleware\SurveillanceUI::class])->group(function () {
// Surveillance UI routes
});
Access the UI
Visit /surveillance-ui (or your configured path) to see the dashboard. Authenticate via Laravel’s default auth (e.g., auth:api or web middleware).
IP, User Agent, Timestamp) to narrow down logs.config/surveillance-ui.php (customize paths, middleware, or UI behavior).resources/views/vendor/surveillance-ui/ (override default views if needed).app/Http/Middleware/SurveillanceUI.php (extend logic for route protection).auth:api) alongside the UI middleware:
Route::middleware(['auth:api', \Neelkanth\SurveillanceUI\Middleware\SurveillanceUI::class])->get('/surveillance', [SurveillanceController::class, 'index']);
can:admin:
Route::middleware(['auth', 'can:admin'])->group(function () {
// Surveillance UI routes
});
cp vendor/neelkanth/surveillance-ui/resources/views/vendor/surveillance-ui/dashboard.blade.php resources/views/vendor/surveillance-ui/
@extends('vendor.surveillance-ui.layout')
@section('content')
{{ parent::section('content') }}
<div class="card">
<h5>Custom Widget</h5>
<!-- Your content -->
</div>
@endsection
Surveillance facade to fetch logs in your controllers:
use Neelkanth\Surveillance\Facades\Surveillance;
$logs = Surveillance::logs()->latest()->take(100)->get();
public function customView(Request $request) {
$data = Surveillance::logs()->where('ip', $request->ip)->get();
return view('custom.surveillance-view', compact('data'));
}
Surveillance\Events\LogCreated to trigger actions (e.g., notifications):
Surveillance::onLogCreated(function ($log) {
// Send Slack/email alert
event(new SurveillanceAlert($log));
});
$searchResults = Surveillance::logs()->search($query)->get();
Echo.channel('surveillance-logs')
.listen('LogCreated', (log) => {
// Update UI dynamically
});
Route::get('/api/surveillance/logs', function () {
return Surveillance::logs()->latest()->paginate(20);
});
Middleware Order Matters
SurveillanceUI middleware after auth middleware to avoid infinite redirects:
// Wrong: Redirect loop
['auth', \Neelkanth\SurveillanceUI\Middleware\SurveillanceUI::class]
// Correct:
['web', \Neelkanth\SurveillanceUI\Middleware\SurveillanceUI::class]
Asset Paths in Production
public/vendor/surveillance-ui/ is symlinked or copied to public/:
php artisan storage:link # If using storage for assets
php artisan view:clear
Performance with Large Logs
'logs_per_page' => 50, // Default is 20
surveillance_logs table for ip, user_id, and created_at.CSRF Token Mismatch
class SurveillanceUI extends Middleware {
public function handle($request, Closure $next) {
if ($request->is('api/*')) {
return $next($request);
}
return $next($request)->unless($request->is('surveillance-ui*'));
}
}
Logs Not Appearing?
laravel-surveillance is recording logs:
php artisan surveillance:list
surveillance_logs table directly in the database.UI Styling Issues
npm run dev # or `npm run prod` for production
resources/sass/app.scss if needed.Permission Denied
view surveillance permission (if using Laravel’s gate):
Gate::define('view-surveillance', function ($user) {
return $user->isAdmin(); // Custom logic
});
Custom Log Fields
surveillance_logs table and hydrate custom fields:
Surveillance::extend(function ($surveillance) {
$surveillance->logCustomField = function ($request) {
return $request->input('custom_field');
};
});
IP Blocking Automation
Surveillance::blockIp($ip, 'Suspicious activity detected');
Localization
php artisan vendor:publish --tag=surveillance-ui-lang
resources/lang/{locale}/surveillance-ui.php.Testing
Surveillance::fake()->log('test-event', ['key' => 'value']);
$response = $this->actingAs($user)->get('/surveillance-ui');
$response->assertSee('test-event');
Backup Strategy
Surveillance::logs()->where('created_at', '<', now()->subDays(30))->delete();
$schedule->command('surveillance:prune')->daily();
How can I help you explore Laravel packages today?