11

我在 Laravel 5.3 中创建了调用视图的 Mailable 类。但是,我需要将一些变量从我的 Controller 传递给 Mailable 类,然后在 View 中使用这些值。这是我的设置:

控制器:

$mailData = array(
                   'userId'     => $result['user_id'],
                   'action'     => $result['user_action'],
                   'object'     => $result['user_object'],
                  );
Mail::send(new Notification($mailData));

可邮寄:

class Notification extends Mailable
{
    use Queueable, SerializesModels;

    protected $mailData;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($mailData)
    {
        $this->$mailData = $mailData;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // Array for Blade
        $input = array(
                          'action'     => $mailData['action'],
                          'object'     => $mailData['object'],
                      );

        return $this->view('emails.notification')
                    ->with([
                        'inputs' => $this->input,
                      ]);
    }
}

以上给了我错误:

ErrorException in Notification.php line 25:
Array to string conversion

参考constructMailable Class 中的行:

$this->$mailData = $mailData;

我在这里做错了什么?如何正确地将数组值从Controllerto传递Mailable,然后使用with将它们传递给View?

4

1 回答 1

14

试试这个:

public $mailData;

public function __construct($mailData)
{
    $this->mailData = $mailData;
}

public function build()
{
    // Array for Blade
    $input = array(
                      'action'     => $this->mailData['action'],
                      'object'     => $this->mailData['object'],
                  );

    return $this->view('emails.notification')
                ->with([
                    'inputs' => $input,
                  ]);
}

文档

于 2016-11-17T11:44:35.590 回答