How to add $record and $ownerRecord in a relation manager notification
Hi everyone!
I'm trying to add the record and the owner record name in the success notification on the create action.
This is what i tried but it's not working.
Notification::make()->success()
->title( fn (RelationManager $livewire, Model $record) => "{$record->name} successfully added to {$livewire->ownerRecord->name}." )
8 Replies
would it not be fn (Model $record) rather than livewire?
something like fn (Status $record): string =>
substr($record->name).....
substr($record->name).....
Inside a relation manager you have two record:
- RelationManager $livewire->ownerRecord give you access to the owner record
- Model $record is the pivot record
I can use it fine like that to customize the header but it's not working when i try to change the success notification body
Your code snippet above is missing the ->send() method though.
It’s inside a successNotification so I just have to specify the notification
@charleswilfriedk can you show your whole action code?
I suspect you may need to get what you need from an outer closure, rather than the one on the notification. So like ...
Maybe
My code right now is like that
AttachAction::make()
->modalHeading(
fn (RelationManager $livewire) => "Attach a role to user {$livewire->ownerRecord->name}"
)
->successNotification(
Notification::make()
->success()
->title(
fn (RelationManager $livewire, Model $record) =>
"The role {$record->name} was successfully attached to the user {$livewire->ownerRecord->name}."
)
)
Let me try your solution
AttachAction::make()
->modalHeading(
fn (RelationManager $livewire) => "Attach a role to user {$livewire->ownerRecord->name}"
)
->successNotification(function (RelationManager $livewire, Model $record) {
return Notification::make()
->success()
->title("The role {$record->name} was successfully attached to the user {$livewire->ownerRecord->name}.");
})
It's working!!
Thx
Yup. The Notification callbacks don't know anything about relation managers. As per that discussion you tagged me in, the parameters available in a callback closure are very context dependent.
I get it now