英文:
Laravel Filament: Send notification to the assigned user
问题
我在Filament中安装了一个名为"Ticketing"的插件:https://filamentphp.com/plugins/ticketing
Filament有一个名为afterCreate()
的方法,那是我通常实现通知API的地方,就像Filament文档中所述:
https://filamentphp.com/docs/2.x/notifications/database-notifications
到目前为止,这是我所做的:
$record = $this->record;
// $recipients = User::pluck('name', 'id');
// 发送数据库通知(在网站内)
foreach ($recipients as $recipient) {
$recipient->notify(
Notification::make()
->title('New Incident Created')
->icon('heroicon-o-document-text')
->body(
"**New incident: " .
__($record->ir_number->prefix ?? 'IR#' . str_pad($record->ir_number, 4, '0', STR_PAD_LEFT)) .
" of type {$record->caseTypeRelationship->name} was created by " . auth()->user()->name . "**"
)
->actions([
Action::make('View')
->url(TicketResource::getUrl('view', ['record' => $record])),
])
->toDatabase(),
);
};
以下是输入的样子:
Select::make('assigned_to_id')
->label(__('Assign Ticket To'))
->hint(__('Key in 3 or more characters to begin search'))
->searchable()
->getSearchResultsUsing(function ($search) {
if (strlen($search) < 3) {
return [];
}
return config('filament-ticketing.user-model')::where('name', 'like', "%{$search}%")
->limit(50)
->pluck('name', 'id');
})
->getOptionLabelUsing(fn ($value): ?string => config('filament-ticketing.user-model')::find($value)?->name)
->disabled($cannotAssignTickets);
如何将$recipients
设置为assign_to_id
输入中选择的用户?
英文:
I installed a plugin in filament called "Ticketing": https://filamentphp.com/plugins/ticketing
filament has a method called afterCreate()
, that's where I usually implement the notifications API, just as Filament docs mention: <br>https://filamentphp.com/docs/2.x/notifications/database-notifications
This is what I did so far:
$record = $this->record;
// $recipients = User::pluck('name', 'id');
// Send Database notification (within website)
foreach ($recipients as $recipient) {
$recipient->notify(
Notification::make()
->title('New Incident Created')
->icon('heroicon-o-document-text')
->body(
"**New incident: " .
__($record->ir_number->prefix ?? 'IR#' . str_pad($record->ir_number, 4, '0', STR_PAD_LEFT)) .
" of type {$record->caseTypeRelationship->name} was created by " . auth()->user()->name . "**"
)
->actions([
Action::make('View')
->url(TicketResource::getUrl('view', ['record' => $record])),
])
->toDatabase(),
);
};
Here is what the input looks like:
Select::make('assigned_to_id')
->label(__('Assign Ticket To'))
->hint(__('Key in 3 or more characters to begin search'))
->searchable()
->getSearchResultsUsing(function ($search) {
if (strlen($search) < 3) {
return [];
}
return config('filament-ticketing.user-model')::where('name', 'like', "%{$search}%")
->limit(50)
// ->get()
// ->filter(fn ($user) => $user->can('manageAssignedTickets', Ticket::class))
->pluck('name', 'id');
})
->getOptionLabelUsing(fn ($value): ?string => config('filament-ticketing.user-model')::find($value)?->name)
->disabled($cannotAssignTickets),
// ->hiddenOn('create')
]
How can I set $recipients
to equal the selected user in the assign_to_id input?
答案1
得分: 2
$recipient = User::find($assigned_to_id);
Notification::make()
->title('新建事件已创建')
->icon('heroicon-o-document-text')
->body(
"**新建事件:**" .
__($record->ir_number->prefix ?? 'IR#' . str_pad($record->ir_number, 4, '0', STR_PAD_LEFT)) .
",类型为{$record->caseTypeRelationship->name},由" . auth()->user()->name . "创建"
)
->actions([
Action::make('查看')
->url(TicketResource::getUrl('view', ['record' => $record])),
])
->sendToDatabase($recipient);
英文:
im new to filamentphp so please correct me if i;m wrong, maybe you know that $recipients need a collection not a builder. same as
User::where('id',1)->get() //is a builder
and
User::find(1) //is a collection
so in my case, I use this :
`
$record = $this->record;
$assigned_to_id= $record->assigned_to_id;
$recipient = User::find($assigned_to_id);
Notification::make()
->title('New Incident Created')
->icon('heroicon-o-document-text')
->body(
"**New incident: " .
__($record->ir_number->prefix ?? 'IR#' . str_pad($record->ir_number, 4, '0', STR_PAD_LEFT)) .
" of type {$record->caseTypeRelationship->name} was created by " . auth()->user()->name . "**"
)
->actions([
Action::make('View')
->url(TicketResource::getUrl('view', ['record' => $record])),
])
->sendToDatabase($recipient);
`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论