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
codeartisan
codeartisan3mo ago
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 == 'superadmin@coffeeims.com';
}),
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 == 'superadmin@coffeeims.com';
}),
Want results from more Discord servers?
Add your server
More Posts
Performances issues when Filament goes in productionHello, I have an API that does real time on a specific domain. On this laravel application I've addForm validation messagesI have a simple form in Filament v3, but the validation errors shows as a default browser message (smake export file download when we run queue:work command and csv file is generatedHi! i want to make my csv file download whenever i run queue:work command and also store the path ofFilament widget on click show table list recordIs it possible to make the widget clickable so that when you click it, it redirects you to a resourcTyped static property error when trying to activate cluster functionalityI'm trying to use the cluster functionality but I'm running into an error I can't get past. The clusHow to collapse section on button clickI have a livewire component that is rendering the form I want that when form is submited I collapse using @livewireScriptConfig instead of @filamentScriptsI'm using the tables package in livewire components. I'm using an extra Alpine plugin. So followingHow do custom field : Dynamic datalist + Input arrayHello, I try to migrate my old backend to filament. The last thing I need to do is the one in the viCan I defer uploading via the FileUpload component until form submissionI'm using Filament's file upload component in my form within my livewire component: https://filamentHow to attach and detach a relation in case there were valuesMy resource **may** has One to One (Polymorphic) relationship with Review, I want to create a review