0

我正在尝试使用此方法将 HTML 模板发送到 MailTrap

public function send($result_id)
    {

         $result = Result::whereHas('user', function ($query) {
                 $query->whereId(auth()->id());
             })->findOrFail($result_id);

        \Mail::to('test@eam.com')->send(new ResultMail);

        return view('client.result', compact('result'))->withStatus('Your test result has been sent successfully!');

    }

与 result.blade.file

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Test No. {{ $result->id }}</title>
        <link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
        <style type="text/css">
            html {
                margin: 0;
            }
            body {
                background-color: #FFFFFF;
                font-size: 10px;
                margin: 36pt;
            }
        </style>
    </head>
    <body>
        <p class="mt-5">Total points: {{ $result->total_points }} points</p>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Question Text</th>
                    <th>Points</th>
                </tr>
            </thead>
            <tbody>
                @foreach($result->questions as $question)
                    <tr>
                        <td>{{ $question->question_text }}</td>
                        <td>{{ $question->pivot->points }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </body>
</html>

但我得到一个错误

未定义变量:结果(查看:C:\Users\USER\Documents\Laravel-Test-Result-PDF-master\Laravel-Test-Result-PDF-master\resources\views\client\result.blade.php)

4

1 回答 1

1

好吧,据我所知,您需要有 Mailable 类,并且从 mailable 类中,您需要返回视图并在那里传递数据。你的可邮寄课程应该

class ResultMail extends Mailable
{
  use Queueable, SerializesModels;

  public $result;

  /**
   * Create a new message instance.
   *
   */
    public function __construct($result)
    {
        $this->result = $result;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {

        return $this->view('client.result');
    }
}

应该是这样的。然后你需要将数据传递给ResultMail

\Mail::to('test@eam.com')->send(new ResultMail($result));
于 2020-05-10T20:43:41.770 回答