简体   繁体   中英

Laravel problem passing variables from controller to mailer

I am having trouble passing variables from the controller to the mail fucntion I have tried to search it up on google but didn't find anything that works my goal is to get the variable from the form to the welcomemail.blade the current issue is that the variable doesnt get to my wlcomemail.php from the controller. the mailing part itself does work.

Controller code:

$email_data = array(
            'first_name'=>'John',
            'last_name'=>'Doe',
            'email'=>'john@doe.com',
            'password'=>'temp',
        );

        //Customer::create($data);
        Mail::to($email_data['email'])->send(new welcomemail($email_data)); 

welcomemail.php code:


namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class welcomemail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    public $email_data;
    public $example;

    public function __construct()
    {
        $this->email_data = $email_data;
        $this->example = 'example';
    }


    public function build()
    {
        return $this->markdown('emails.welcomemail');
    }
}

blade code:

# Introduction

Welcome.
{{$email_data['first_name']}}
{{$example}}
@endcomponent

change this:

public function __construct()
{
    $this->email_data = $email_data;
    $this->example = 'example';
}

to:

public function __construct($email_data)
{
    $this->email_data = $email_data;
    $this->example = 'example';
}

now you have it in your mail class

it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM