Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Data from Laravel Controller to Mailable Class

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:

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 construct line in Mailable Class:

$this->$mailData = $mailData;

What have I got wrong here? How do I correctly pass array values from Controller to Mailable and then use with to pass them on to the View?

like image 916
Neel Avatar asked Nov 17 '16 11:11

Neel


People also ask

How do you pass data into an email template in laravel?

Laravel will pass all public properties of the mailable class to the email view. Mail::to($sendto)->send(new SendSBTorecipient($sb)); then in SendSBTorecipient add the SB to the constructor.

What is mailable class in Laravel?

Mailable class in Laravel will abstract the level of building emails. Meaning, mailable class is responsible for collecting data and passing that data to a view. All you have to do is instantiate the mailing API and tell it which mailable to use. Mail::to('[email protected]')->send(new WelcomeEmail);

How Laravel send email?

Laravel uses free feature-rich library SwiftMailer to send emails. Using the library function, we can easily send emails without too many hassles. The e-mail templates are loaded in the same way as views, which means you can use the Blade syntax and inject data into your templates. Sends email.


1 Answers

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

like image 136
Rimon Khan Avatar answered Oct 09 '22 19:10

Rimon Khan