简体   繁体   中英

Passing variables from controller to mail class - Laravel

I'm trying to pass data from my controller to mail class but it won't work for some reasons.

Problem should be somewhere in variable passing but I don't have any error messages.

Controller code:

$send_data = [  
    "name" => $product->name,  
    "quantity" => $item->quantity,  
    "price" => $final_price  
];  

\Mail::to($email)->send(new OrderMail($send_data));  

Mail class:

class OrderMail extends Mailable  
{
use Queueable, SerializesModels;

public $subject = "Úspešná objednávka";
public $send_data;

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

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

I would like to access data in emails.ordered view

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

$this->send_data      // this is a variable that you can use in view.

You can access through $send_data variable (that is defined in __construct() ).

After all I was just stupid and made typo in my markdown file, here's how my markdown should looked:

@component('mail::message')

<h1>Dobrý deň {{$buyer}}!</h1> 
<p>Vaša objednávka bola spracovaná úspešne.<p>
@component('mail::table')
| Produkt         | Počet ks.            | Cena               |
|:---------------:|:--------------------:|:------------------:|
@foreach($send_data as $item)
|{{$item["name"]}}| {{$item["quantity"]}}| {{$item["price"]}}€|
@endforeach
<p>V prípade osobného odberu si môžete vyzdvihnúť tovar na predajni s týmto 
klúčom: {{$next_id}}</p>
@endcomponent
@endcomponent

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