亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用字符串鍵解壓數組 - 將多個非模型數據傳遞到郵件視圖

無法使用字符串鍵解壓數組 - 將多個非模型數據傳遞到郵件視圖

PHP
千萬里不及你 2023-06-24 17:41:53
我正在嘗試將非模型數據傳遞到我的電子郵件刀片。然而我不斷收到這個錯誤。Cannot?unpack?array?with?string?keys這是我的控制器:$data = ['email'=> $email, 'token'=> $token, 'name'=> $name];? ? ? ? sendMailWithMailerClass($email, '\App\Mail\ApplicantSetPasswordMail', $data);? ? ? ??...function sendMailWithMailerClass($mailTo, $mailerClass, $mailerClassParams){??? ? try{? ? ? ? Mail::to($mailTo)->send(new $mailerClass(...$mailerClassParams));? ? } catch (Exception $e) {? ? ? ?? ? }}電子郵件課程如下<?phpnamespace App\Mail;use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;use Illuminate\Contracts\Queue\ShouldQueue;class ApplicantSetPasswordMail extends Mailable{? ? use Queueable, SerializesModels;? ? public $data;? ??? ? public function __construct($data)? ? {? ? ? ? $this->data = $data;? ? }? ??? ? public function build()? ? {? ? ? ? return $this->markdown('emails.applicant_set_password', compact('data'));? ? }}這是我的電子郵件視圖:<!DOCTYPE html><html ><head></head><body ><center>? ??? ? <p>Dear {{ $data['name'] }},</p>? ? <p style="margin-bottom: 25px;">Text</p>? ? <a href="{{ URL::to('/').'/url/?email='.$data['email'].'&token='.$data['token'] }}" >Set Password</a>? ? ? ? ? ? ? ? ? ? ?</center></body></html>
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

...您嘗試在線使用的splat 運算符new $mailerClass(...$mailerClassParams)無法與像您這樣的關聯數組一起使用$data


我可以看到您用于可郵寄類的構造函數是public function __construct($data)這樣您應該能夠使用new $mailerClass($mailerClassParams)


如果您確實有一個在構造函數中具有多個參數的可郵寄類,那么public function __construct($email, $token, $name)您仍然可以將其作為 1 個數組參數傳遞,并檢查傳遞的數組的內容。或使用new $mailerClass(...array_values($mailerClassParams)). 但是,請注意,如果您使用 end up using ,array_values()那么數組的順序實際上很重要,因為這就是它將如何映射參數,以便$mailerClassParams數組的第一個條目始終是第一個參數,因此這不是推薦的方式。


function sendMailWithMailerClass($mailTo, $mailerClass, $mailerClassParams)

{

    try{

        // Remove ... splat operator here

        Mail::to($mailTo)->send(new $mailerClass($mailerClassParams));

    } catch (Exception $e) {

       

    }

}


// make sure all your $mailClass constructors take 1 parameter

public function __construct($data)

{

    $this->data = $data;

}

或者


function sendMailWithMailerClass($mailTo, $mailerClass, $mailerClassParams)

{

    try{

        // Remove keys of the associative array with array_values

        Mail::to($mailTo)->send(new $mailerClass(...array_values($mailerClassParams)));

    } catch (Exception $e) {

       

    }

}


// make sure all constructor takes correct parameters in the correct order

public function __construct($email, $token, $name)

{

    $this->data = [

        'email' => $email,

        'token' => $token,

        'name' => $name,

    ];

}


查看完整回答
反對 回復 2023-06-24
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

它是從 PHP 8.1 開始實現的。


$args = [

? ? ...['z' => 2, 'x' => 3],

? ? ...['y' => 1, 'x' => 4],

];


class Foo

{

? ? public function __construct(int $x, int $y, int $z)

? ? {

? ? ? ? echo <<<OUTPUT

? ? ? ? ? ? x = $x

? ? ? ? ? ? y = $y

? ? ? ? ? ? z = $z

? ? ? ? OUTPUT;

? ? }

}


new Foo(...$args);

/* Output:

?* x = 4

?* y = 1

?* z = 2

?*/

在線測試一下!


查看完整回答
反對 回復 2023-06-24
  • 2 回答
  • 0 關注
  • 240 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號