2017-05-22 9 views
1

私はL5.3を使用しています。Laravel削除後のキュー可能通知モデルが動作しない

私はQueueable Laravel Notificationsをたくさん持っていますが、彼らはRedisでうまく動作しています。 Eloquent Modelの削除に関する通知を除きます。ここで

は、通知ソースの例です:

<?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->notify(new Deleted($id));

私は、通知の作品をimplements ShouldQueueを削除した場合。しかし、私はいくつかの外部サービス(テレグラム、Facebook、ワン信号など)を使用しているためキューが必要であり、いくつかの通知を多くのユーザーに送ることができます。

storage\logs\laravel.logstorage\logs\worker.logファイルにもエラーはありません。 failed_jobsの項目はありません。

はい、私はモデルのsoft deletingを使用しています。

答えて

0

[OK]を、最終的に私は両方を掲示、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. 

これは私が使用することを決定したものです:私たちは10個のチャンネルを持っている場合、我々は2通知チャネル、10回を持っている場合

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

第二の方法を使用して、我々は、DBから2回ゲームのモデルを取得します。これはオーバーヘッドですが、Eloquent Collectionヘルパーなどを使用することができます。これが、配列の代わりに2番目のメソッドを選択した理由です。私はこれがいつか誰かを助けることを願っています。

私はコンストラクタでEloquentモデルを取得できない理由を聞いてうれしいです。

関連する問題