Using hint that the record is edited.

I want to show the input field is updated but after refresh the updated is gone. Is there any way I can make this code works
No description
Solution:
So you need to store that information somewhere in the cache or database
Jump to solution
14 Replies
Topherllobrera
TopherllobreraOP2mo ago
TextInput::make('first_name') ->label('First Name') ->hintcolor('primary') ->hint(fn($record) => $record && $record->wasChanged('first_name') ? 'Updated' : '') ->placeholder('First Name') ->validationMessages(['required' => 'First name is required']) ->required(), TextInput::make('middle_name') ->label('Middle Name') ->placeholder('Middle Name') ->hintcolor('primary') ->hint(fn($record) => $record && $record->wasChanged('middle_name') ? 'Updated' : ''), TextInput::make('last_name') ->label('Last Name') ->placeholder('Last Name') ->required() ->hintcolor('primary') ->validationMessages(['required' => 'Last name is required']),
Dennis Koch
Dennis Koch2mo ago
But after refresh the updated is gone.
After what kind of refresh? ->wasChanged() is only for the last saved data on the model. What are you trying to achieve?
Bruno Pereira
Bruno Pereira2mo ago
What I think he wants is: If the form has 5 inputs and you only change 2 of them, after you press the submit/save changes button the hint turns to updated because the response returns the model info, but after you refresh the page (F5) the hint defaults to '' because the request doesnt return the model with the wasChanged info.
Topherllobrera
TopherllobreraOP2mo ago
Coolman understand it very well! @Dennis Koch hope you can help
Topherllobrera
TopherllobreraOP2mo ago
Something like this
No description
Topherllobrera
TopherllobreraOP2mo ago
The goal is the other user can see any changes!
Bruno Pereira
Bruno Pereira2mo ago
I don't think you can do it out of the box with filament because the data is request based. I would try to use websockets to notify the other users that that model with id = x was changed.
Topherllobrera
TopherllobreraOP2mo ago
Alright, the other solution I think is adding another column in database comparing the updated data. The downside is the speed of the app.
Dennis Koch
Dennis Koch2mo ago
I still don't really understand what's the benefit of this. You want to show "Updated" forever once something was updated? I think once you refresh the page it's okay to loose that info?
Topherllobrera
TopherllobreraOP2mo ago
there is also status condition whenever the status is approve it will go back to normal state, removing the hint. The goal is to let the other user view the changes faster. My user is lazy.
Bruno Pereira
Bruno Pereira2mo ago
Yeah I would choose webscokets to notify the user if anyone changed that model while I'm on the page, then I would put a button to refresh/get the most recent data, aka, just a page refresh. With webscokets you can make a alert informing that someone is also editing that model like you have in some stores "there's 3 more people looking at this product"
Solution
Dennis Koch
Dennis Koch2mo ago
So you need to store that information somewhere in the cache or database
Dennis Koch
Dennis Koch2mo ago
Or if you care about notifying other looking at it the same time, see Coolman's response.
Topherllobrera
TopherllobreraOP2mo ago
I think I can use spatie activity logs. I will reply and update you regarding the result this is what I did
class ActivityHelper { public static function activityChanges($attribute, $get, $state) { $currentValue = $get($attribute); $personalInfoId = $get('id'); $status = $get('status'); if (!$personalInfoId) { return null; } if ($status === 'Approved') { // Delete activity log for this ID Activity::where('subject_type', 'App\Models\PersonalInformation') ->where('subject_id', $personalInfoId) ->delete(); return null; } $latestActivity = Activity::query() ->where('subject_type', 'App\Models\PersonalInformation') ->where('subject_id', $personalInfoId) // Specific to this personal info record ->where('event', 'updated') ->whereJsonContains("properties->attributes->$attribute", $currentValue) ->whereRaw("JSON_EXTRACT(properties, '$.attributes.$attribute') != JSON_EXTRACT(properties, '$.old.$attribute')") ->latest() ->first(); return $latestActivity ? 'Updated' : null; } }`
and call the hint ->hintcolor('primary')->hint(fn ($state, $get) => ActivityHelper::activityChanges('children_record', $get, $state))
Want results from more Discord servers?
Add your server