在Laravel 5.4中创建一个简单的联系表单

时间:2018-08-29 16:01:53

标签: php laravel laravel-5.4 contactus

我正在一个网站上,我正在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),因为已经定义了错误,所以我不确定为什么会收到此错误。

1 个答案:

答案 0 :(得分:0)

根据documentationSendMailable类中的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);
}