猿问

无法使用字符串键解压数组 - 将多个非模型数据传递到邮件视图

我正在尝试将非模型数据传递到我的电子邮件刀片。然而我不断收到这个错误。

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) {

       

    }

}

电子邮件课程如下

<?php


namespace 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>


千万里不及你
浏览 268回答 2
2回答

白猪掌柜的

...您尝试在线使用的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){&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; // Remove ... splat operator here&nbsp; &nbsp; &nbsp; &nbsp; Mail::to($mailTo)->send(new $mailerClass($mailerClassParams));&nbsp; &nbsp; } catch (Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}// make sure all your $mailClass constructors take 1 parameterpublic function __construct($data){&nbsp; &nbsp; $this->data = $data;}或者function sendMailWithMailerClass($mailTo, $mailerClass, $mailerClassParams){&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; // Remove keys of the associative array with array_values&nbsp; &nbsp; &nbsp; &nbsp; Mail::to($mailTo)->send(new $mailerClass(...array_values($mailerClassParams)));&nbsp; &nbsp; } catch (Exception $e) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }}// make sure all constructor takes correct parameters in the correct orderpublic function __construct($email, $token, $name){&nbsp; &nbsp; $this->data = [&nbsp; &nbsp; &nbsp; &nbsp; 'email' => $email,&nbsp; &nbsp; &nbsp; &nbsp; 'token' => $token,&nbsp; &nbsp; &nbsp; &nbsp; 'name' => $name,&nbsp; &nbsp; ];}

至尊宝的传说

它是从 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 */在线测试一下!
随时随地看视频慕课网APP
我要回答