简体   繁体   English

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

[英]Creating a simple contact form in Laravel 5.4

I am working on a website in which I am creating a simple contact form in Laravel 5.4 我在一个网站上工作,该网站正在Laravel 5.4中创建一个简单的联系表

I have used the following in SendMailable class: 我在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');
    }
}

And in controller I am using the following: 在控制器中,我使用以下内容:

<?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
        ]);
   }
}

The blade emails.posting-message has the following content: 刀片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>

In the routes, I am using the following: 在路线中,我使用以下方法:

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


Problem Statement: 问题陈述:

The error which I am getting now is Undefined property: Illuminate\\View\\Engines\\CompilerEngine::$fullname (View: /home/vagrant/code/search/resources/views/emails/posting-message.blade.php) I am not sure why I am getting this error as I have made the definition of it. 我现在得到的错误是Undefined property: Illuminate\\View\\Engines\\CompilerEngine::$fullname (View: /home/vagrant/code/search/resources/views/emails/posting-message.blade.php)不知道为什么我定义了这个错误,为什么会出现这个错误。

As per the documentation , the properties in you SendMailable class that are public will be automatically made available in your blade file. 根据文档SendMailable类中public的属性将自动在刀片文件中可用。

You can then access those properties as if you'd passed them in the data array ie in your blade file change $this->fullname to just $fullname . 然后,您可以访问这些属性,就像您将它们传递到数据数组中一样,即在刀片文件中,将$this->fullname更改$this->fullname $fullname

You're blade file should then look something like: 您的刀片文件应如下所示:

<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>

If you're going to be using the get route you have set up to see what it looks like in the browser then you'll need to add some dummy data to the array that is getting passed to the view. 如果要使用设置的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM