我正在嘗試向 other_emails 發送電子郵件,這樣當用戶 1 已經為他的朋友預訂時,用戶 1 收到了電子郵件,他的朋友也收到了一封電子郵件,我嘗試使用 foreach 向 other_emails 發送電子郵件,但我得到了這個錯誤資訊Undefined variable: isi_email1
我該如何解決?
控制器:
$isi_email1 = [
'title'=>'Thank you for your purchase',
'body'=>'Please give us your review about your experience using Pintunuswantara Travel. Link : https://pintunuswantara.com/review'
];
$subject = [$booking->email];
Mail::to($subject)->send(new OthersEmail($isi_email1));
$others = Booking::select('other_emails');
foreach($others as $other){
Mail::to($other)->send(new OthersEmail($isi_email1));
}
其他郵箱:
public $isi_email1;
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
$this->isi_email1 = $isi_email1;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('[email protected]')
->subject('Thank you')
->view('other_email');
}
uj5u.com熱心網友回復:
問題似乎是變數 scope/context。為了讓您在類中的函式中訪問變數,您必須將其作為引數傳入;它不繼承其呼叫位置的背景關系。
IE:在這個例子中,你的OthersEmail::construct函式必須接受$isi_email1引數才能將它分配給它的屬性。
將建構式更改為如下所示,以便分配并能夠傳遞變數:
public function __construct(array $isi_email1) {
$this->isi_email1 = $isi_email1;
}
如果您不打算使用型別化引數,則可以array從引數定義中洗掉 。
public function __construct($isi_email1) {
// etc.
}
您的控制器類已經在嘗試傳遞變數,因此不必在那里進行任何更改即可使其作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/534849.html
標籤:PHP拉维
