我試圖在laravel 5.1中排隊發送電子郵件的電子郵件,我通過一個名爲invoice
的變量,當我在Job類中dd($invoice->dateString())
它返回正確的值但是當我通過它時在查看$invoice
變量返回空數組(所以我得到一個關於嘗試從非對象獲取屬性的錯誤...)。laravel 5.1排隊電子郵件發送的問題
我遇到的第二個問題是當我嘗試添加附件到作業時,它返回一個錯誤:「關閉序列化失敗:不允許」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];
}
有人可以告訴我什麼我做錯了什麼? 非常感謝你們所有人:)
我想你是正確的隊友,有關如何存儲這些數據供以後使用的任何建議? – benjah
是啊!這是它,謝謝很多人!那附件的另一件事情呢?任何線索? – benjah
由於無法序列化SplFileInfo,只需將附件文件路徑傳入use()即可。 – Michael