Get data in ->after() on a custom action

I have the following code. I would like to get the newly created record to use as a redirect in the ->after() function. If any one could help I would appreciate it. Also if I am doing this a long way please let me know.
Action::make('Log Today')
->button()
->action(function (Action $action){
$tenantId = Filament::getTenant()->id;
$today = date('Y-m-d');
$day = Day::firstOrCreate(
['date' => $today, 'person_id' => $tenantId],
['date' => now()]
);
})
->after(function () {
Notification::make()
->success()
->title('Day Record Created!')
->send();

return redirect()->route('filament.app.resources.days.view',[
'tenant' => Filament::getTenant(),
'record' => Day::where('date',date('Y-m-d'))->where('person_id',Filament::getTenant()->id)->first(),
]);
})
Action::make('Log Today')
->button()
->action(function (Action $action){
$tenantId = Filament::getTenant()->id;
$today = date('Y-m-d');
$day = Day::firstOrCreate(
['date' => $today, 'person_id' => $tenantId],
['date' => now()]
);
})
->after(function () {
Notification::make()
->success()
->title('Day Record Created!')
->send();

return redirect()->route('filament.app.resources.days.view',[
'tenant' => Filament::getTenant(),
'record' => Day::where('date',date('Y-m-d'))->where('person_id',Filament::getTenant()->id)->first(),
]);
})
13 Replies
Vp
Vp11mo ago
->after(function (Model $record) {})
David | Fortune Validator
App\Filament\Resources\DayResource\Pages\ListDays::App\Filament\Resources\DayResource\Pages\{closure}(): Argument #1 ($record) must be of type App\Models\Day, null given, called in /Users/davidwhicker/Development/Herd/allergy_tracker/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
App\Filament\Resources\DayResource\Pages\ListDays::App\Filament\Resources\DayResource\Pages\{closure}(): Argument #1 ($record) must be of type App\Models\Day, null given, called in /Users/davidwhicker/Development/Herd/allergy_tracker/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
For Record this is on the List page with a custom header action. Doesn't look like its passed the created object across.
Vp
Vp11mo ago
Weird, you can notification and redirect inside action itself if after() not working, like this
->action(function (Action $action){
$tenantId = Filament::getTenant()->id;
$today = date('Y-m-d');
$day = Day::firstOrCreate(
['date' => $today, 'person_id' => $tenantId],
['date' => now()]
);

Notification::make()
->success()
->title('Day Record Created!')
->send();

return redirect()->route('filament.app.resources.days.view',[
'tenant' => Filament::getTenant(),
'record' => $day->id, // change according to yours
]);
})
->action(function (Action $action){
$tenantId = Filament::getTenant()->id;
$today = date('Y-m-d');
$day = Day::firstOrCreate(
['date' => $today, 'person_id' => $tenantId],
['date' => now()]
);

Notification::make()
->success()
->title('Day Record Created!')
->send();

return redirect()->route('filament.app.resources.days.view',[
'tenant' => Filament::getTenant(),
'record' => $day->id, // change according to yours
]);
})
And one thing, here in Model Did you use Model (use Illuminate\Database\Eloquent\Model;) or your Day model? I use actual Model and I don't have any error
David | Fortune Validator
I did have it as Day but I just tried Model and have the same error. I did have this to begin with but as I am using the new ->unsavedChangesAlerts() on v3.2 it is popping up saying I have unsaved before redirecting which ruins the flow.
Vp
Vp11mo ago
If you try like this, then what did you get?
->after(function (Model $record) {
dd($record);
})
->after(function (Model $record) {
dd($record);
})
The error in here seems like you passed wrong data in $record (return redirect() -> route('view', ['record' => 'wrong']);)
David | Fortune Validator
App\Filament\Resources\DayResource\Pages\ListDays::App\Filament\Resources\DayResource\Pages\{closure}(): Argument #1 ($record) must be of type Illuminate\Database\Eloquent\Model, null given, called in /Users/davidwhicker/Development/Herd/allergy_tracker/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
App\Filament\Resources\DayResource\Pages\ListDays::App\Filament\Resources\DayResource\Pages\{closure}(): Argument #1 ($record) must be of type Illuminate\Database\Eloquent\Model, null given, called in /Users/davidwhicker/Development/Herd/allergy_tracker/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
and I used use Illuminate\Database\Eloquent\Model;
<?php

namespace App\Filament\Resources\DayResource\Pages;

use App\Filament\Resources\DayResource;
use App\Models\Day;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Resources\Pages\ListRecords;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Model;

class ListDays extends ListRecords
{
protected static string $resource = DayResource::class;

protected function getHeaderActions(): array
{
return [
Action::make('Log Today')->label("Log Today")
->button()
->action(function (Action $action){
$tenantId = Filament::getTenant()->id;
$today = date('Y-m-d');

// Use firstOrCreate for more efficient and readable code
$day = Day::firstOrCreate(
['date' => $today, 'person_id' => $tenantId],
['date' => now()]
);
})
->after(function (Model $record) {
dd($record);
})
<?php

namespace App\Filament\Resources\DayResource\Pages;

use App\Filament\Resources\DayResource;
use App\Models\Day;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Resources\Pages\ListRecords;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Model;

class ListDays extends ListRecords
{
protected static string $resource = DayResource::class;

protected function getHeaderActions(): array
{
return [
Action::make('Log Today')->label("Log Today")
->button()
->action(function (Action $action){
$tenantId = Filament::getTenant()->id;
$today = date('Y-m-d');

// Use firstOrCreate for more efficient and readable code
$day = Day::firstOrCreate(
['date' => $today, 'person_id' => $tenantId],
['date' => now()]
);
})
->after(function (Model $record) {
dd($record);
})
Vp
Vp11mo ago
Yeah, understood.. this is custom data, I don't know how to pass the record but get the last record from after like this
->after(function () {
$day = Day::latest()->first();

Notification:: make()
-> success()
-> title('Day Record Created!')
-> send();

return redirect() -> route('filament.app.resources.days.view', [
'tenant' => Filament:: getTenant(),
'record' => $day->id,
]);
})
->after(function () {
$day = Day::latest()->first();

Notification:: make()
-> success()
-> title('Day Record Created!')
-> send();

return redirect() -> route('filament.app.resources.days.view', [
'tenant' => Filament:: getTenant(),
'record' => $day->id,
]);
})
David | Fortune Validator
No problem, thank you for the assit and the code snippet. Much appreciated
Vp
Vp11mo ago
One thing, I have this in my code hope this works for you too, try $this->record inside after() without importing (Model $record)
David | Fortune Validator
Property [$record] not found on component: [app.filament.resources.day-resource.pages.list-days]
Property [$record] not found on component: [app.filament.resources.day-resource.pages.list-days]
oh well, was worth a shot are you doing this inside the ListRecord page? in the protected function getHeaderActions() ?
Vp
Vp11mo ago
Yes inside list, now i dont know any other solution
David | Fortune Validator
strange, I wonder why it works okay for you and not myself. Its not the end of the world though, the code I have works. thanks again
Vp
Vp11mo ago
😆 it's because the actions perform different, maybe
Want results from more Discord servers?
Add your server