How to know when a record had been displayed

Evening all, I've got a 'has seen' to populate when a table row is displayed, (Messages table basically, want to record when message has been displayed) Looked into all the 'hooks' I can find. Haven't seen anything that allows me to call a function on a table render etc. Got no idea where to start on this...gratefully receive suggestions!
Solution:
Ok, since not using Spatie Activity Log just yet, couldn't do that. But, did lead indirectly to a solution, which was to simply leverage a table action ->visible closure to do the work: ```...
Jump to solution
7 Replies
awcodes
awcodes2w ago
What’s is the use case?
Matthew
MatthewOP2w ago
It's an internal messaging system, (and for legal reasons, I've never heard of WhatsApp...)
No description
Andrew Wallo
Andrew Wallo2w ago
Are you talking about Filament tables? Can you show an example? You can maybe use the "rendered" lifecycle hook from Livewire: https://livewire.laravel.com/docs/lifecycle-hooks
Laravel
Lifecycle Hooks | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Matthew
MatthewOP2w ago
That is just a filament table. I've had a look through the various hooks in the Filament Docs, but couldn't find anything for table load\view etc. I can have a look at Livewire, but I expect that means stepping outside of the panel builder options, and into a custom page\component ?
toeknee
toeknee2w ago
We do this for senstive information, and what I did was user Spatie Acitvity Log and say for a record action I did:
Tables\Actions\ViewAction::make()->mutateRecordDataUsing(
function ($record, $data) {
activity()
->causedBy(auth()->user())
->performedOn($record)
->event('view')
->log('Viewed Account Details');

return $data;

}
),
Tables\Actions\ViewAction::make()->mutateRecordDataUsing(
function ($record, $data) {
activity()
->causedBy(auth()->user())
->performedOn($record)
->event('view')
->log('Viewed Account Details');

return $data;

}
),
and for a page/list view I did:
public function render(): View
{

activity()
->causedBy(auth()->user())
->performedOn(new \App\Models\Accounts)
->event('view')
->log('viewed list of accounts under team: '.auth()->user()->currentTeam->name.' with id: '.auth()->user()->current_team_id);

return parent::render();
}
public function render(): View
{

activity()
->causedBy(auth()->user())
->performedOn(new \App\Models\Accounts)
->event('view')
->log('viewed list of accounts under team: '.auth()->user()->currentTeam->name.' with id: '.auth()->user()->current_team_id);

return parent::render();
}
Matthew
MatthewOP2w ago
Thanks @toeknee, shall check it out.
Solution
Matthew
Matthew7d ago
Ok, since not using Spatie Activity Log just yet, couldn't do that. But, did lead indirectly to a solution, which was to simply leverage a table action ->visible closure to do the work:
public static function getTableActions() : array
{
return [
Action::make('ReadReceipt')
->visible(function ($record) : bool {

if ($record->ime_seen == false && $record->ime_sender_type === enumImeSenderType::CLIENT) {
$record->ime_seen = now();
$record->ime_seen_by = getPersonaUser()->getId();
$record->save();
}

return false;

}),
];
}
public static function getTableActions() : array
{
return [
Action::make('ReadReceipt')
->visible(function ($record) : bool {

if ($record->ime_seen == false && $record->ime_sender_type === enumImeSenderType::CLIENT) {
$record->ime_seen = now();
$record->ime_seen_by = getPersonaUser()->getId();
$record->save();
}

return false;

}),
];
}

Did you find this page helpful?