0

I am working on a website in which I am creating a simple contact form in Laravel 5.4

I have used the following in SendMailable class:

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

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

john
  • 11,311
  • 40
  • 131
  • 251
  • 2
    `$this` in your Blade file is not an instance of `PostingMessageController`, so accessing `$this->fullName` is trying to access the property of the `CompilerEngine` (rendering class of a `.blade.php` file). You need to pass it to that view as a variable, say `$that`, then access `$that->fullName`. (Or similar) – Tim Lewis Aug 29 '18 at 16:06
  • @TimLewis I change $this to $that in the blade. Is there anywhere else I need to make changes ? – john Aug 29 '18 at 16:12
  • Yup, also need to pass `that` to your view, so `return $this->view('email.layoutOne', ["that" => $this])...` Note: is that the right file name? Shouldn't it be `emails.posting-message`? – Tim Lewis Aug 29 '18 at 16:13
  • @TimLewis Corrected. It still says undefined variable that. – john Aug 29 '18 at 16:14
  • `$this->view('email.posting-message', ["that" => $this])`? Just a heads up, I haven't used this format for sending emails before, so the signature might be different for `$this->view()` vs `view()->with(...)`, etc. – Tim Lewis Aug 29 '18 at 16:15
  • @TimLewis May be I am missing something. – john Aug 29 '18 at 16:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179043/discussion-between-john-and-tim-lewis). – john Aug 29 '18 at 16:16

1 Answers1

0

As per the documentation, the properties in you SendMailable class that are public will be automatically made available in your blade file.

You can then access those properties as if you'd passed them in the data array i.e. in your blade file change $this->fullname to just $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.

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);
}
Rwd
  • 34,180
  • 6
  • 64
  • 78
  • I have copied the blade code as it is and it doesn't seem to work. – john Aug 29 '18 at 18:15
  • @john Are you definitely making a `post` request or are you just viewing it in the browser? – Rwd Aug 29 '18 at 18:15
  • @john I've seen the routes that you've added to your question, however, you also have a `GET` request that isn't passing anything to the view which would cause an error so I just wanted to make sure you're definitely making a `POST` request to that route i.e. with ajax or a form submission? Or are you just viewing it in the browser? – Rwd Aug 29 '18 at 18:19
  • For now, I am viewing in the browser but will have a form like [this](https://search.ruckify.com/posting) in the future. – john Aug 29 '18 at 18:21
  • @john Then I'm going to assume that is where your issue is. If you're just going to `/posting-message` in the browser it's going to be making a `GET` request (not a `POST` request) so in this case it's going to use the `showContactUs` method in the controller and the `$data` array you're passing to your view is empty. I'll add an update to my answer. – Rwd Aug 29 '18 at 18:26
  • @john Did this answer your question or is it still not working? – Rwd Aug 30 '18 at 06:24