尝试Mail :: send时未定义的变量

时间:2016-08-22 10:23:30

标签: php laravel laravel-4

我正在尝试将结果插入到我的电子邮件视图中,然后使用foreach显示数据,但我保持

  

“未定义的变量:结果”

。我怎么能解决这个问题?

这是我的控制器代码:

$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);
            });

这是我的刀片:

@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

2 个答案:

答案 0 :(得分:3)

请参阅:https://laravel.com/docs/5.2/mail#sending-mail

$结果未通过:

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

您需要将其传递为:

$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);
        });

答案 1 :(得分:0)

试试这个:

$data = array('mailTo' => $serviceProviderEmail, 'subject' => $subject ,'mailBody' => $message);
Mail::send('template_name', ['data' => $data], function ($message) use ($data) {
    $message->from('no-reply@xyz.com', 'xyz');
    $message->to($data['mailTo'])->subject($data['subject']);
});

我正在使用这种语法,它工作正常。