如何将数据从 Laravel 控制器发送到 Mailable Class

How to send Data from Laravel Controller to Mailable Class

我在调用视图的 Laravel 5.3 中创建了 Mailable Class。但是,我需要将一些变量从我的 Controller 传递到 Mailable Class,然后在 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

参考 Mailable 中的 construct 行 Class:

$this->$mailData = $mailData;

我哪里错了?如何正确地将数组值从 Controller 传递到 Mailable,然后使用 with 将它们传递到 View

试试这个:

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,
                  ]);
}

Docs