如何将数据从 Laravel 控制器发送到可邮寄类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40653747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:45:54  来源:igfitidea点击:

How to send Data from Laravel Controller to Mailable Class

laravellaravel-5.3laravel-mail

提问by Neel

I have created Mailable Class in Laravel 5.3 which calls the view. However, I need to pass some variables from my Controller to the Mailable Class and then use these values inside the View. This is my set-up:

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

Controller:

控制器:

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

Mailable:

可邮寄:

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

The above gives me the error:

以上给了我错误:

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

Referring to the constructline in Mailable Class:

参考constructMailable Class 中的行:

$this->$mailData = $mailData;

What have I got wrong here? How do I correctly pass array values from Controllerto Mailableand then use withto pass them on to the View?

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

回答by Rimon Khan

Try this:

尝试这个:

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

文档