2015-09-02 59 views
2

我試圖在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]; 
    } 

有人可以告訴我什麼我做錯了什麼? 非常感謝你們所有人:)

回答

1

只是一個猜測......但是當使用Mail :: queue()時,$ data被轉換/轉換爲一個數組/你失去了視圖內的對象 - 因此,爲什麼你嘗試調用方法()時會收到錯誤,因爲它們不存在。

不是通過發票+更新對象,而是通過句柄方法從它們中獲取所需內容並構造$ data數組。

$data = [ 
    'invoice_foo' => $invoice->getFoo(), 
    'invoice_bar' => $invoice->getBar(), 
]; 

***道歉,如果這根本沒有幫助!

+0

我想你是正確的隊友,有關如何存儲這些數據供以後使用的任何建議? – benjah

+0

是啊!這是它,謝謝很多人!那附件的另一件事情呢?任何線索? – benjah

+0

由於無法序列化SplFileInfo,只需將附件文件路徑傳入use()即可。 – Michael

1

,所以我找到了答案感謝@邁克爾,我已經改變了我的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函數內調用,而不是在刀片視圖模板中調用。