Search code examples
phphtmllaravelformslaravel-11

Laravel 11 The form data is not getting passed to the email template


I am fairly new to using Laravel. But the question is here, I am having an issue where the data that is getting from the form after clicking submit, it is not getting passed to a blade template. For example the email sends to the email address, but inside the email the name from the form does not display in the email. I should say I am using Laravel with Livewire.

One of the Inputs for first_name:

 <x-wui-input wire:model="first_name" placeholder="First Name" label="First Name *[Required]" type="text" id="firstName" name="first_name" class="block w-full px-4 py-4 mx-1 mt-1 border-gray-300 rounded-md focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm" />

The Email Blade Template:

<div class="p-4 bg-white rounded-lg shadow-md">
    <h2 class="mb-4 text-2xl font-bold text-gray-800">Thank You for Submitting a Good Deed!</h2>
    <p class="mb-2 text-gray-600">Thank you {{$data['first_name']}} {{$data['last_name']}}  for nomination for the annual Chasing Good Award.</p>
</div>

The Form Component (Submit method):

public function submit(Request $request)
    {
        $this->validate();

        $data = [
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'nominee_name' => $request->nominee_name
        ];

        Nomination::create(
            $this->only([
                'first_name',
                'last_name',
                'email_address',
                'phone_number',
                'nominating_category',
                'nominee_name',
                'nominee_email',
                'nj_county',
                'story_essay',
                'uploaded_video',
                'disclaimer_agreed'
            ])
        );

        // Create a new Mailable instance
        $mailable = new ThankYouMailable($data);
        $mailabe2 = new NominationMailable($data);
        $mailable3 = new ThankYouSelfMailable($data);

        if ($this->email_address == $this->nominee_email) {
            Mail::to($this->email_address)->send($mailable3);
        } else {
            Mail::to($this->email_address)->send($mailable);
            Mail::to($this->nominee_email)->send($mailabe2);
        }

        session()->flash('message', 'Good deed submitted successfully!');

        return back()->with('message', 'Good deed submitted successfully!');
    }

The Mailable Class:

class ThankYouSelfMailable extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function build()
    {
        return $this->view('emails.thank_you_self_nominator')->with($this->data, $this->data);
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Thank You For Submitting',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.thank_you_self_nominator',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

Solution

  • In a livewire component you don't use the request object, instead you should use $this:

    $data = [
        'first_name' => $this->first_name,
        'last_name' => $this->last_name,
        'nominee_name' => $this->nominee_name
    ];