Home > Back-end >  Undefined variable in build function
Undefined variable in build function

Time:04-25

I am trying to pass a $pdfPath variable in order to send the email with the attachment. But somehow it keeps on saying undefined variable even though I already make a public variable in the mail file. Glad to have any help on this. Thank you.

<?php

namespace App\Mail;

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

class GiftHospitalityCompleted extends Mailable
{
    use Queueable, SerializesModels;

    public $pdfPath;
    public $ticket;
    public $members;
    public $url;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($pdfPath,$ticket,$members,$url)
    {
        $this->pdfPath = $pdfPath;
        $this->ticket = $ticket;
        $this->members = $members;
        $this->url = $url;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Service Ticket Completed')->markdown('mails.GiftHospitalityCompleted')->attach($pdfPath);
    }
}

CodePudding user response:

The problem is your parameter passing build function. Try to use the below code :

public function build()
{
    return $this->subject('Service Ticket Completed')->markdown('mails.GiftHospitalityCompleted')->attach($this->pdfPath);
}

CodePudding user response:

The solution is to not include the attach method in the build function, but actually place it on the _construct function.

public function __construct($pdfPath,$ticket,$members,$url)
    {
        $this->attach($pdfPath);
        $this->ticket = $ticket;
        $this->members = $members;
        $this->url = $url;
    }
  • Related