Text Input Column Notification

Hi I would like to display a notification when a Table Text Input Column is updated, I couldn't see anything in the documentation so I thought I would try my luck here. Thanks in Advance
15 Replies
Dennis Koch
Dennis Koch5mo ago
You can overwrite the update process via ->updateStatUsing() and add the Notification in there.
MazeEzam
MazeEzam5mo ago
Thanks Dennis, I'll give that a go. So I attempted to use the suggested ->updateStateUsing() using a closure to return the notification and this give me the functionality that I need however now the new value entered into the column text input field doesn't update to the DB. ->updateStateUsing(function() {Notification::make() ->title(' Prices Updated') ->success() ->send();}) also I couldn't find any info on this method in the documentation, maybe I need to also pass the new value of the field to this method?
Dennis Koch
Dennis Koch5mo ago
however now the new value entered into the column text input field doesn't update to the DB.
Right. The columns is called updateStateUsing(). The method you pass is used to update the value. If you don't update the value within that method it only sends the notification.
MazeEzam
MazeEzam5mo ago
Ok I thought this might be the case, thank you for following up, much appreciated. As I am reasonably new to all this, how do I pass the value to the method? Thanks
Dennis Koch
Dennis Koch5mo ago
Check the implementatio of TextInputColumn and just copy over the default functionality of updateStateUsing(). Add you Notification code at the end of it.
MazeEzam
MazeEzam5mo ago
Ok thanks, I'll dig into the code and have a look. Thanks again. Righto, I think I'm really confused now. the only reference to updateStateUsing() I could find is in CanUpdateState.php but I cannot figure out what I need to copy over, as I mentioned I am reasonably new to all this. Any help would be appreciated. Thanks
Dennis Koch
Dennis Koch5mo ago
updateState() is the method that is executed. It either runs the method passed by updateStateUsing() or the default below. If you remove all the edge cases it would be something like:
->updateStateUsing(function ($state) {
$livewire = $this->getLivewire();
$record = $this->getRecord();

$columnName = $this->getName();

if (! ($record instanceof Model)) {
return null;
}

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code

return $state;
});
->updateStateUsing(function ($state) {
$livewire = $this->getLivewire();
$record = $this->getRecord();

$columnName = $this->getName();

if (! ($record instanceof Model)) {
return null;
}

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code

return $state;
});
MazeEzam
MazeEzam5mo ago
Thanks again, I have loaded the above into my table
->updateStateUsing(function($state) {

$livewire = $this->getLivewire();
$record = $this->getRecord();
$columnName = $this->getName();

if (! ($record instanceof Model)) {
return null;
}

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code
Notification::make()
->title(' Prices Updated')
->success()
->send();

return $state;
})
->updateStateUsing(function($state) {

$livewire = $this->getLivewire();
$record = $this->getRecord();
$columnName = $this->getName();

if (! ($record instanceof Model)) {
return null;
}

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code
Notification::make()
->title(' Prices Updated')
->success()
->send();

return $state;
})
MazeEzam
MazeEzam5mo ago
And now I receive this error https://flareapp.io/share/67OZGVZP
Flare
Method App\Filament\Resources\CustomerResource\RelationManagers\AdjustmentsRelationManager::getLivewire does not exist. - The error occurred at http://localhost:8000/customers/1/edit
MazeEzam
MazeEzam5mo ago
Thinking I need to import a class however I am not sure which class it needs.
Dennis Koch
Dennis Koch5mo ago
Ah, thought so. You probably need ->updateStateUsing(function($state, $component) { and use $component instead of $this
MazeEzam
MazeEzam5mo ago
Hi again, so I have updated as per the above but now I have this error https://flareapp.io/share/xPQZbnX7
Flare
An attempt was made to evaluate a closure for [Filament\Tables\Columns\TextInputColumn], but [$component] was unresolvable. - The error occurred at http://localhost:8000/customers/1/edit
MazeEzam
MazeEzam5mo ago
->updateStateUsing(function($state, $component) {
$livewire = $component->getLivewire();
$record = $component->getRecord();
$columnName = $component->getName();

if (! ($record instanceof Model)) {
return null;
}

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code
Notification::make()
->title(' Prices Updated')
->success()
->send();

return $state;
->updateStateUsing(function($state, $component) {
$livewire = $component->getLivewire();
$record = $component->getRecord();
$columnName = $component->getName();

if (! ($record instanceof Model)) {
return null;
}

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code
Notification::make()
->title(' Prices Updated')
->success()
->send();

return $state;
Copy of the updated code. So I think I have fixed this but using a different approach. It now updates the record in the DB and sends me the notification, only fires if the price has been changed. Is there something else I should be thinking about here though?? Thanks
->updateStateUsing(function($state, $record) {

// Column Name
$columnName = 'new_price';

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code
Notification::make()
->title(' Prices Updated')
->success()
->send();

return $state;
})
->updateStateUsing(function($state, $record) {

// Column Name
$columnName = 'new_price';

$record->setAttribute($columnName, $state);
$record->save();

// Your notification code
Notification::make()
->title(' Prices Updated')
->success()
->send();

return $state;
})
Dennis Koch
Dennis Koch5mo ago
Looks good to me. Sorry. Thought $component was always injected.
MazeEzam
MazeEzam5mo ago
All good, thank you for all your help, much appreciated. 👍