Laravel Filament: 向指定用户发送通知

huangapple go评论83阅读模式
英文:

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-&gt;record;

        // $recipients = User::pluck(&#39;name&#39;, &#39;id&#39;);

        // Send Database notification (within website)
        foreach ($recipients as $recipient) {
            $recipient-&gt;notify(
                Notification::make()
                    -&gt;title(&#39;New Incident Created&#39;)
                    -&gt;icon(&#39;heroicon-o-document-text&#39;)
                    -&gt;body(
                        &quot;**New incident: &quot; .
                            __($record-&gt;ir_number-&gt;prefix ?? &#39;IR#&#39; . str_pad($record-&gt;ir_number, 4, &#39;0&#39;, STR_PAD_LEFT)) .
                            &quot; of type {$record-&gt;caseTypeRelationship-&gt;name} was created by &quot; . auth()-&gt;user()-&gt;name . &quot;**&quot;
                    )
                    -&gt;actions([
                        Action::make(&#39;View&#39;)
                            -&gt;url(TicketResource::getUrl(&#39;view&#39;, [&#39;record&#39; =&gt; $record])),
                    ])
                    -&gt;toDatabase(),
            );
        };

Here is what the input looks like:

Select::make(&#39;assigned_to_id&#39;)
  -&gt;label(__(&#39;Assign Ticket To&#39;))
  -&gt;hint(__(&#39;Key in 3 or more characters to begin search&#39;))
  -&gt;searchable()
  -&gt;getSearchResultsUsing(function ($search) {
      if (strlen($search) &lt; 3) {
        return [];
      }

      return config(&#39;filament-ticketing.user-model&#39;)::where(&#39;name&#39;, &#39;like&#39;, &quot;%{$search}%&quot;)
        -&gt;limit(50)
        // -&gt;get()
        // -&gt;filter(fn ($user) =&gt; $user-&gt;can(&#39;manageAssignedTickets&#39;, Ticket::class))
        -&gt;pluck(&#39;name&#39;, &#39;id&#39;);
   })
    -&gt;getOptionLabelUsing(fn ($value): ?string =&gt; config(&#39;filament-ticketing.user-model&#39;)::find($value)?-&gt;name)
    -&gt;disabled($cannotAssignTickets),
    // -&gt;hiddenOn(&#39;create&#39;)
    ]

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()
        -&gt;title(&#39;New Incident Created&#39;)
                -&gt;icon(&#39;heroicon-o-document-text&#39;)
                -&gt;body(
                    &quot;**New incident: &quot; .
                        __($record-&gt;ir_number-&gt;prefix ?? &#39;IR#&#39; . str_pad($record-&gt;ir_number, 4, &#39;0&#39;, STR_PAD_LEFT)) .
                        &quot; of type {$record-&gt;caseTypeRelationship-&gt;name} was created by &quot; . auth()-&gt;user()-&gt;name . &quot;**&quot;
                )
                -&gt;actions([
                    Action::make(&#39;View&#39;)
                        -&gt;url(TicketResource::getUrl(&#39;view&#39;, [&#39;record&#39; =&gt; $record])),
                ])
        -&gt;sendToDatabase($recipient);

`

huangapple
  • 本文由 发表于 2023年3月12日 18:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定