Filament: Override Page Render to Log User Activity

2025-01-22

In Filament application, you may need to log some activities when rendering the page. Let's see how to do it using a spatie/laravel-activitylog package.


You can overwrite a render() on any of the Filament Pages to add custom logic.

Just add your logic, and then call parent::render() at the end.

For example, you want to log that the User visited the View page of another user.

In this example, we log activity, set the performedOn($this->record) to the current record, and add a custom log message.

app/Filament/Resources/UserResource/ViewUser.php:

use Illuminate\View\View;
use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
 
class ViewUser extends ViewRecord
{
protected static string $resource = UserResource::class;
 
protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
];
}
 
public function render(): View
{
activity()
->causedBy(auth()->user())
->performedOn($this->record)
->event('view')
->log('User '. auth()->user()->name . ' viewed a user ' . $this->record->name);
 
return parent::render();
}
}

This behavior isn't specific to Resource pages in app/Filament/Resources. You can also override render() in this way on other custom pages.

A few of our Premium Examples: