我正在尝试排队在laravel 5.1中发送发票电子邮件的电子邮件,我传入一个名为invoice
的变量,当在Job类中我dd($invoice->dateString())
时,它返回正确的值但是当我通过时它在视图中$invoice
变量返回空数组(所以我得到一个关于尝试从非对象获取属性的错误...)。
我遇到的第二个问题是,当我尝试向作业添加附件时,它返回错误:“关闭序列化失败:不允许序列化'SplFileInfo'。”
工作类看起来像这样:
namespace LM2\Jobs;
use Guzzle\Service\Client;
use LM2\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use LM2\Models\User as User;
use LM2\Models\Client as LMClient;
class SendInvoiceEmail extends Job implements SelfHandling, ShouldQueue
{
protected $user;
protected $invoice;
protected $attachment;
protected $update;
public function __construct(User $user, LMClient $client, $invoice,$update)
{
$this->user = $user;
$this->client = $client;
$this->invoice = $invoice;
$this->update = $update;
}
public function handle()
{
$attachment = $this->client->invoiceFile($this->invoice->id,['vendor' => 'Test','product' => 'Your Product']);
$invoice = $this->invoice;
$data = [
'invoice' => $this->invoice,
'update'=> $this->update,
];
$user = $this->user;
\Mail::queue('emails.invoices', $data , function($m) use ($user,$invoice,$attachment){
$m->to($user->email)->subject('New payment received')->attach($attachment);
});
}
}
我的控制器功能如下:
public function sendEmailInvoice($update = false){
$client = \Auth::client();
$user = \Auth::user();
$invoices = $client->invoices();
$this->dispatch(new SendInvoiceEmail($user,$client,$invoices[0],$update));
$activity = $data['update'] ? 'updated': 'added';
return ['success', $activity];
}
有人可以告诉我,我做错了什么? 非常感谢大家:))
答案 0 :(得分:2)
只是一个猜测......但是当使用Mail :: queue()时,$ data get被转换/强制转换为数组/你丢失了视图中的对象 - 这就是为什么你在尝试调用方法时收到错误的原因(),因为它们不存在。
不是传递发票+更新对象,而是在handle方法中获取所需内容并构造$ data数组。
$data = [
'invoice_foo' => $invoice->getFoo(),
'invoice_bar' => $invoice->getBar(),
];
***如果这根本没有帮助,请道歉!
答案 1 :(得分:1)
所以我找到答案感谢@Michael,我已经改变了我的handle
所以它现在看起来像这样:
public function handle(Mailer $mailer)
{
$client = $this->client;
$invoice = $this->invoice;
$data = [
'date' => $invoice->dateString(),
'amount' => $invoice->dollars(),
'update'=> $this->update,
];
$user = $this->user;
return $mailer->queue('emails.invoices', $data , function($m) use ($user,$client,$invoice){
$attachment = $client->invoiceFile($invoice->id,['vendor' => 'Infogamy','product' => 'Your Product']);
$m->to($user->email)->subject('New payment received')->attach($attachment);
});
}
应该在邮件程序回调函数内处理附件,并且应该在$invoice
函数内调用从handle
变量(对象)调用的函数,而不是在刀片视图模板中调用。