F
Filament12mo ago
Jocka

Change a resource attribute/property from an action.

Hey guys, can i somehow change a resource property/attribute from an action like this?
->actions([
Tables\Actions\ViewAction::make()
->action(function () {
$this->contact->read_at = Carbon::now();
$this->contact->save();
}),
Tables\Actions\DeleteAction::make(),
])
->actions([
Tables\Actions\ViewAction::make()
->action(function () {
$this->contact->read_at = Carbon::now();
$this->contact->save();
}),
Tables\Actions\DeleteAction::make(),
])
if not, how can i achieve this?
7 Replies
Jocka
JockaOP12mo ago
The idea is to make a read/unread functionality so when the user clicks on a row in the table it marks it as read.
krekas
krekas12mo ago
i think this way you overwrite the view action. maybe on the view class in the mount() method you could set the read_at
Jocka
JockaOP12mo ago
could you maybe give me an example of how you mean? because the view is in a modal so is there a way to do this?
krekas
krekas12mo ago
you didn't mention that that maybe use after() method instead of action
Jocka
JockaOP12mo ago
I created the ViewClass and did as you told me. Thanks a lot
<?php

namespace App\Filament\Resources\ContactResource\Pages;

use App\Filament\Resources\ContactResource;
use Carbon\Carbon;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewContact extends ViewRecord
{
protected static string $resource = ContactResource::class;

public function mount(int | string $record): void
{
$this->record = $this->resolveRecord($record);
$this->record->read_at = Carbon::now();
$this->record->save();
}
}
<?php

namespace App\Filament\Resources\ContactResource\Pages;

use App\Filament\Resources\ContactResource;
use Carbon\Carbon;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewContact extends ViewRecord
{
protected static string $resource = ContactResource::class;

public function mount(int | string $record): void
{
$this->record = $this->resolveRecord($record);
$this->record->read_at = Carbon::now();
$this->record->save();
}
}
krekas
krekas12mo ago
Don't forget to use parent from the method

Did you find this page helpful?