简体   繁体   中英

Passing variables from Controller to Mailables in Laravel [php]

In my UserController I have a function

public function userFollow($id)
    {
        $authuser = Auth::user();
        $authuser->follow($id);
        //mail goes to the followiee ($id)
        $followiee = User::where('id', $id)->first();
        $to = $followiee->email;
        Mail::to($to)->send(new MyMail);
        return redirect()->back()->with('message', 'Following User');
    }

I have also created a Mailable MyMail

class MyMail extends Mailable
{
    use Queueable, SerializesModels;


    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome');
    }
}

Inside my welcome email I need to access some variables like $to which is defined in the UserController

I tried the following in MyMail Mailable:

 public function build()
    {
        return $this->view('emails.welcome',['to' => $to]);
    }

But I am getting Undefined variable: to

How to pass variables from Controller to Mailables?

Update:

What I have tried so far:

UserController

Mail::to($to)->send(new MyMail($to));

MyMail

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

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

Welcome.blade.php

 {{ $to }}

Error:

FatalErrorException in Mailable.php line 442:
[] operator not supported for strings

One solution is to pass variables to the MyMail constructor, like this:

UserController

Mail::to($to)->send(new MyMail($to));

MyMail

public $myTo;

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

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

Welcome.blade.php

{{ $myTo }}

Update: As @Rahul noted in his answer, the $to property can be defined as public. And in this case the view will be populated with it automatically.

Update 2: You just need to give a different name to your $to variable (eg $myTo ) to distinguish it from the $to in the Mailable.php which is defined as public $to = []; .

There are two ways you may make data available to your view.

  1. First, any public property defined on your mailable class will automatically be made available to the view
class MyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $to;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome'); // $to will be automatically passed to your view
    }
}
  1. Second you may manually pass your data to the view via the with method however you will still pass data via the mailable class' constructor; however, you should set this data to protected or private properties so the data is not automatically made available to the template.
class MyMail extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome',['to'=>$to]);


     }
    }

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