Filip J.J. Filip J.J. - 3 months ago 11
PHP Question

Undefined variable when trying to Mail::send

I am trying to insert results into my e-mail view and then use foreach to display data however I keep getting


"Undefined variable: results"


. How could I fix that?

Here is my controller code:

$results = $sales->orderBy('created_at', 'desc')->get();

Mail::send('mailings.emails.' . $view, $data, function($message) use ($person, $results, $title )
{
$message->to($person->peopleEmail, $person->peopleFirstName . ' ' . $person->peopleLastName)->subject($title);
});


Here is my blade:

@foreach($results as $result)
<table class="row">
<tr>
<td class="wrapper last">

<table class="twelve columns">
<tr>
<td>
<h5 style="text-align:center;">{{$result->SaleID}}</h5>
</td>
<td class="expander"></td>
</tr>
</table>

</td>
</tr>
</table>
@endforeach

Answer

Refer this : https://laravel.com/docs/5.2/mail#sending-mail

$results is not being passed:

 Mail::send('mailings.emails.' . $view, $data, function($message) use ($person, $results, $title )
        {
            $message->to($person->peopleEmail, $person->peopleFirstName . ' ' . $person->peopleLastName)->subject($title);
        });

You need to pass it as :

$data = ['results' => $results];//ADD this line

 Mail::send('mailings.emails.' . $view, $data, function($message) use ($person, $results, $title )
        {
            $message->to($person->peopleEmail, $person->peopleFirstName . ' ' . $person->peopleLastName)->subject($title);
        });