Getting access to the request object in filament table actions
When an action is performed I need to log the action but at some point I need to get access the request object
public function createAudit(Request $request, $description, $event_type)
{
return AuditLog::create([
'user_id' => auth()->id(),
'description' => $description,
'event_type' => $event_type,
'business_id' => auth()->user()->business->id,
'branch_id' => auth()->user()->branch->id,
'date' => Carbon::now(),
'ip_address' => $request->ip(),
'user_agent' => $request->userAgent(),
'method' => $request->method(),
]);
}
public function createAudit(Request $request, $description, $event_type)
{
return AuditLog::create([
'user_id' => auth()->id(),
'description' => $description,
'event_type' => $event_type,
'business_id' => auth()->user()->business->id,
'branch_id' => auth()->user()->branch->id,
'date' => Carbon::now(),
'ip_address' => $request->ip(),
'user_agent' => $request->userAgent(),
'method' => $request->method(),
]);
}
1 Reply
solved
You can pass the Request $request in the action as the second parameter
Action::make('delete')
->label('Delete')
->requiresConfirmation()
->color('danger')
->icon('heroicon-o-trash')
->action(function ($record, Request $request) {
// Delete the record
if ($record->delete()) {
try {
//code...
$this->createAudit($request, "Deleted user of name " . $record->name . "", 'DELETED USER');
} catch (\Throwable $th) {
//throw $th;
//dd($th);
}
Notification::make()
->title('Delete record ' . $record->id . ' successfully')
->success()
->send();
}
})
->visible(function ($record) {
return $record->email == '[email protected]';
}),
Action::make('delete')
->label('Delete')
->requiresConfirmation()
->color('danger')
->icon('heroicon-o-trash')
->action(function ($record, Request $request) {
// Delete the record
if ($record->delete()) {
try {
//code...
$this->createAudit($request, "Deleted user of name " . $record->name . "", 'DELETED USER');
} catch (\Throwable $th) {
//throw $th;
//dd($th);
}
Notification::make()
->title('Delete record ' . $record->id . ' successfully')
->success()
->send();
}
})
->visible(function ($record) {
return $record->email == '[email protected]';
}),