2016-11-11 179 views
0

我已經安裝此通知:Laravel 5.3「工作」不工作

<?php 

namespace App\Notifications; 


use Illuminate\Bus\Queueable; 
use Illuminate\Notifications\Notification; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Notifications\Messages\MailMessage; 

class Published extends Notification 
{ 
    use Queueable; 

    /** 
    * Create a new notification instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 

    } 

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

    /** 
    * Get the mail representation of the notification. 
    * 
    * @param mixed $notifiable 
    * @return \Illuminate\Notifications\Messages\MailMessage 
    */ 
    public function toMail($notifiable) 
    { 
     return (new MailMessage) 
        ->from('[email protected]') 
        ->greeting('Hallo!') 
        ->subject('Neue Anfrage @ Ober24.de') 
        ->line('Wir haben ein neues Event für Sie verfügbar.') 
        ->action('Neues Event', 'http://localhost:8000/articles'); 
    } 

    /** 
    * Get the array representation of the notification. 
    * 
    * @param mixed $notifiable 
    * @return array 
    */ 
    public function toArray($notifiable) 
    { 
     return [ 
      // 
     ]; 
    } 
} 

,並在我的QueuesController我有這個

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 

class QueuesController extends Controller 
{ 


    public function index() { 


    $users = \App\User::all(); 

     foreach ($users as $user) { 
      dispatch(new \App\Jobs\SendNewArticleNotification); 
     } 

    } 
} 

我只是不知道要放什麼東西在我SendNewArticleNotification作業處理方法,以便所有用戶都可以收到電子郵件....

<?php 

namespace App\Jobs; 

use Illuminate\Bus\Queueable; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Queue\ShouldQueue; 

class SendNewArticleNotification implements ShouldQueue 
{ 
    use InteractsWithQueue, Queueable, SerializesModels; 

    /** 
    * Create a new job instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     // 
    } 

    /** 
    * Execute the job. 
    * 
    * @return void 
    */ 
    public function handle() 
    { 

     ?????????????????????? 

    } 
} 

$ user-> notify(new \ App \ Notifications \ Published( ));

這行只發送一封郵件,但失敗,可能是因爲$ user變量沒有定義。

如何將$ user變量從控制器傳輸到Job?

回答

0

OK我終於解決了這個問題,所以如果有人需要在這裏是解決方案:

在控制器

,該dispatch(new SendArticleNotification)需要在它的$用戶變量,以便它可以傳遞到這樣的工作

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Jobs\SendNewArticleNotification; 
use App\Http\Requests; 

class QueuesController extends Controller 
{ 


    public function notifyAllUsers() { 

     $users = \App\User::all(); 

     foreach ($users as $user) { 
      dispatch(new SendNewArticleNotification($user)); 
     } 

    } 
} 

然後在作業類的$ user變量只需要被實例化/像這樣

<?php 

namespace App\Jobs; 

use App\Notifications\Published; 
use Illuminate\Bus\Queueable; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Queue\ShouldQueue; 

class SendNewArticleNotification implements ShouldQueue 
{ 
    use InteractsWithQueue, Queueable, SerializesModels; 

    protected $user; 

    /** 
    * Create a new job instance. 
    * 
    * @return void 
    */ 
    public function __construct($user) 
    { 

     $this->user = $user; 

    } 

    /** 
    * Execute the job. 
    * 
    * @return void 
    */ 
    public function handle() 
    { 


     $this->user->notify(new Published()); 

    } 
} 
構建