Monday, May 22, 2017

Laravel Queueable Notification after deleting Model not working

I'm using L5.3.

I have a lot of Queueable Laravel Notifications, they are working fine with Redis. Except notifications related to deleting of some Eloquent Model.

Here is Notification source example:

<?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])
    ];
  }
}

And there is how I send it: $user->notify(new Deleted($id));

If I remove implements ShouldQueue, notification works. But I need queues because of using several external services (like Telegram, Facebook, One signal etc) and some notifications could be sent to lot of users.

There are also no errors in storage\logs\laravel.log and storage\logs\worker.log files. No items in failed_jobs table.

And yes, I am using soft deleting of models.



via John Doe

Advertisement