【问题标题】:$request->contactMail in blade template刀片模板中的 $request->contactMail
【发布时间】:2018-07-26 15:30:00
【问题描述】:

我有 Laravel 内置的联系表格。

使用邮件发送。这是代码:

路线:

Mail::to('contact@example.com')->send(new ContactWebmaster());

联系站长类

return $this->subject('Kontakt forma')->markdown('email.contactwebmaster');

email.contactwebmaster

@component('mail::message')

Thank you for contacting us.. (Example)

@endcomponent

在刀片模板contactwebmaster中,我想通过表单中的输入文本传递用户的电子邮件。

发送邮件的完整路径:

    Route::post('/contactMail', function (Request $request) {
    $validator = Validator::make($request->all(), [
        'kontaktIme' => 'required',
        'kontaktMail' => 'required|email',
        'vasaPoruka' => 'required|max:255',
    ]);
    if ($validator->fails()) {
        return redirect('/kontakt')
            ->withInput($request->input())
            ->withErrors($validator);
    }
    Mail::to('contact@example.com')->send(new ContactWebmaster());
    return redirect('/')->with('message', 'Uspešno ste poslali poruku!');

});

如何做到这一点?我是 Laravel 的新手。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您可以将该数据添加到您的ContactWebmaster 可邮寄:

    Mail::to('contact@example.com')->send(new ContactWebmaster($request->all()));
    

    然后在你的ContactWebmaster:

    public $data;
    
    constructor($data) {
       $this->data = $data;
    }
    
    public function build()
    {
        $this->subject('Kontakt forma')->markdown('email.contactwebmaster', $data);
    }
    

    【讨论】:

      【解决方案2】:

      来自docs

      您可以通过两种方式将数据提供给您的视图。

      通过公共属性:

      public $user;
      
      public function __construct(User $user)
      {
          $this->user= $user;
      }
      

      通过with方法:

      public function build()
      {
          return $this->view('email.contactwebmaster')
                      ->with([
                          'email' => $this->user->email,
                      ]);
      }
      

      【讨论】:

      • 感谢您的回答。我有些不明白。在文档中写了 __contruct(Order $order) 并在顶部使用 App\Order。在我的情况下,我没有订单或用户,我的变量来自联系表单的刀片模板。
      • 这就是您将变量传递给视图的方式
      【解决方案3】:

      试试下面的代码

      Mail::send('view name', ['data'=>$data], function($message)
                          {
                              $message->to('test@gmail.com', 'Email Message')->subject('Enquiry From Page!');
                          }); 
      

      【讨论】:

        猜你喜欢
        • 2014-03-01
        • 1970-01-01
        • 2013-04-29
        • 2015-08-05
        • 2017-04-03
        • 2018-05-31
        • 1970-01-01
        • 2014-08-02
        相关资源
        最近更新 更多