0

我正在一个网站上工作,我在 Laravel 5.4 中创建了一个简单的联系表格

我在 SendMailable 类中使用了以下内容:

<?php
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailable extends Mailable
{
    use Queueable, SerializesModels;
    public $fullname,$phone,$email,$description;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($fullname, $phone, $email,$description)
    {
        $this->fullname = $fullname;
        $this->phone = $phone;
        $this->email = $email;
        $this->description = $description;
    }
    /**
     * Build the message. THIS WILL FORMAT YOUR OUTPUT
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('email.posting-message')->subject('Contact Us Subject');
    }
}

在控制器中,我使用以下内容:

<?php 
Namespace App\Http\Controllers;
use View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use App\Mail\SendMailable;
use Illuminate\Support\Facades\Redirect; 

class PostingMessageController extends Controller
{
    public function showContactUs()
    {
        $data = [];
       return View::make('emails.posting-message',$data);
    }

    public function doContactUs(Request $r)
    {
     $fullname = $r->get('fullName');
     $phone    = $r->get('phone');
     $email    = $r->get('email');
     $description = $r->get('message');
     Mail::to('RECEIVER_EMAIL_ADDRESS')->send(new SendMailable($fullname, $phone, $email, $description));

      if (Mail::failures())
      {
        $message1 = " Something Went Wrong.";
      }
      else
      {
        $message2 = " Message Sent Successfully.";
      }


   return redirect()->route('route_name')->with([
            'warning' => $message1,
            'success' => $message2
        ]);
   }
}

刀片 emails.posting-message 包含以下内容:

<div> 
<p>Fullname : {{ $this->fullname }}</p> 
<p>Phone No. : {{ $this->phone }}</p> 
<p>Email Address : {{ $this->email }}</p> 
<p>Destination : {{ $this->destination }}</p> 
<p>Description : {{ $this->description }}</p> 
<hr> 
<p>Thank you for your Query. We'll get back to you within 24 Hours. </p> 
</div>

在路线中,我使用以下内容:

Route::get('posting-message', 'PostingMessageController@showContactUs')->name('route_name');
Route::post('posting-message', 'PostingMessageController@doContactUs')->name('route_name');


问题陈述:

我现在得到的错误是Undefined property: Illuminate\View\Engines\CompilerEngine::$fullname (View: /home/vagrant/code/search/resources/views/emails/posting-message.blade.php)我不确定为什么会得到这个错误,因为我已经定义了它。

4

1 回答 1

0

根据文档SendMailable,您的类中的属性public将自动在您的刀片文件中可用。

然后,您可以访问这些属性,就像您在数据数组中传递它们一样,即在您的刀片文件中更改$this->fullname$fullname.

你的刀片文件应该看起来像:

<div>
    <p>Fullname : {{ $fullname }}</p>
    <p>Phone No. : {{ $phone }}</p>
    <p>Email Address : {{ $email }}</p>
    <p>Destination : {{ $destination }}</p>
    <p>Description : {{ $description }}</p>
    <hr>
    <p>Thank you for your Query. We'll get back to you within 24 Hours. </p>
</div>

如果您要使用get您设置的路由来查看它在浏览器中的外观,那么您需要向传递给视图的数组添加一些虚拟数据。

public function showContactUs()
{
    $data = [
        'fullname'    => 'John Doe',
        'phone'       => '123456',
        'email'       => 'john.doe@example.com',
        'destination' => 'somewhere far, far away',
        'description' => 'blah blah blah',
    ];

   return View::make('emails.posting-message',$data);
}
于 2018-08-29T17:08:24.970 回答