【问题标题】:Laravel Queueable Notification after deleting Model not working删除模型后 Laravel 可排队通知不起作用
【发布时间】:2017-05-22 22:21:48
【问题描述】:

我正在使用 L5.3。

我有很多可排队的 Laravel 通知,它们在 Redis 上运行良好。删除某些 Eloquent 模型的通知除外。

这里是通知源示例:

<?php

namespace App\Notifications\Games;

use App\Helpers\NotificationHelper;
use App\Game;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Log;

class Deleted extends Notification implements ShouldQueue
{
  use Queueable;

  public $game;
  private $game_id;

  public function __construct($game_id)
  {
    $this->game_id = $game_id;
    $this->game = Game::withTrashed()->with('situation')->find($game_id);
    // Log::info($this->game) here shows everything is ok
  }

  /**
   * Get the notification's delivery channels.
   *
   * @param  mixed $notifiable
   * @return array
   */
  public function via($notifiable)
  {
    return ['database'];
  }

  /**
   * Get the array representation of the notification.
   *
   * @param  mixed $notifiable
   * @return array
   */
  public function toDatabase($notifiable)
  {
    return [
      'game_id' => $this->game->id,
      'html' => '' . view('notifications.games.deleted', ['id' => $this->id, 'game' => $this->game, 'notifiable' => $notifiable])
    ];
  }
}

还有我如何发送它: $user-&gt;notify(new Deleted($id));

如果我删除 implements ShouldQueue,通知会起作用。但由于使用了多种外部服务(例如 Telegram、Facebook、One signal 等),我需要排队,并且可以将一些通知发送给很多用户。

storage\logs\laravel.logstorage\logs\worker.log 文件中也没有错误。 failed_jobs 表中没有项目。

是的,我正在使用 soft deleting 的模型。

【问题讨论】:

    标签: php laravel laravel-5 notifications laravel-5.3


    【解决方案1】:

    好的,我终于找到了2个解决方案,都发布了:

    这不是一个真正的解决方案,对我来说更像是一个肮脏的黑客,因为根据情况它可能会让你编写更多的代码。

    // Change Eloquent to Array in constructor
    $this->game = Game::withTrashed()->with('situation')->find($game_id)->toArray();
    
    // And then change all your toDatabase, toMail, toFacebook etc methods: use $game['name'] instead of $game->name etc.
    

    这是我决定使用的:

    // Remove $game from constructor...
    public function __construct($game_id)
      {
        $this->game_id = $game_id;
      }
    
    // Then add it to your toDatabase, toMail, toFacebook etc methods:
    public function toDatabase($notifiable)
      {
        $game = Game::withTrashed()->with('situation')->find($this->game_id);
        return [
          'game_id' => $this->game_id,
          'html' => '' . view('notifications.games.deleted', ['id' => $this->id, 'game' => $game, 'notifiable' => $notifiable])
        ];
      }
    

    使用第二种方法,如果我们有 2 个通知通道,我们将从 DB 中检索游戏模型 2 次,如果我们有 10 个通道,则检索 10 次。是的,这是开销,但我们可以使用所有 Eloquent Collection 助手等。这个这就是为什么我选择第二种方法而不是数组的原因。我希望有一天这对某人有所帮助。

    我很高兴听到为什么我们不能在构造函数中获得 Eloquent 模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-12
      • 2021-07-03
      • 1970-01-01
      • 2017-10-16
      • 2021-04-05
      • 1970-01-01
      • 2013-12-28
      • 2016-10-13
      相关资源
      最近更新 更多