Laravel 5.3 "Job" 无效
Laravel 5.3 "Job" doesn't work
我已经设置了这个通知:
<?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('support@ober24.de')
->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?
好的,我终于解决了这个问题,如果有人需要,这里是解决方案:
在控制器中,dispatch(new SendArticleNotification)
需要在其中包含 $user 变量,以便它可以像这样传递给作业
<?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));
}
}
}
然后在 Job class 中 $user 变量只需要像这样 instantiated/constructed
<?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());
}
}
我已经设置了这个通知:
<?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('support@ober24.de')
->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?
好的,我终于解决了这个问题,如果有人需要,这里是解决方案:
在控制器中,dispatch(new SendArticleNotification)
需要在其中包含 $user 变量,以便它可以像这样传递给作业
<?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));
}
}
}
然后在 Job class 中 $user 变量只需要像这样 instantiated/constructed
<?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());
}
}