Reputation: 103
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
Upvotes: 1
Views: 1596
Reputation: 424
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()
).
Upvotes: 3
Reputation: 103
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
Upvotes: 0