1

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

4
  • 1
    it won't work is not a good reason. What's not working exactly? Any error messages? Commented Feb 9, 2019 at 19:09
  • nope, I don't have any. I just can't access it in view because the variable is not passed there Commented Feb 9, 2019 at 19:19
  • Could you show your mailable view? Commented Feb 9, 2019 at 19:28
  • What version of Laravel are you using? Can you show how you've tried to access those variables in the blade file? Commented Feb 9, 2019 at 19:38

2 Answers 2

3
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()).

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.