Monday, May 22, 2017

Laravel Undefined Variable: donor ErrorException in d03bd104494df21919e458a02c3243e2b323db06.php line 3:

I have a controller which mainly has two functions. First function adds the user to the database and sends an email (receipt.blade.php) to the user.The second function allows the user to get all the receipts for a particular email that a user enters.

Right now, if I directly go to the page where the user can enter the email and get all receipts, its working fine. But, if I try to do it through the process of adding a new user I get the error described in the title after I click submit to adding the user. However, it has added the user to the database, but shows the error because its sending the email which is the receipt.blade.php and has the undefined variable. My controller has these:

use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;

The first Function is:

public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
            'first_name'=> $data->first_name,
            'last_name' => $data->last_name,
            'email' => $data->email,
            'video_count' => $video_count,
            'amount_donated' => $data->amount_donated,
});

$mailer
            ->to($data->input('email'))
            ->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
                ($data->input('amount_donated'))));

return redirect()->action('PagesController@thank1');
    }

My mailing file(mailable) is:

class MyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $first_name, $last_name, $amount_donated;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($first_name,$last_name,$amount_donated)
    {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->amount_donated = $amount_donated;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('myemailid12341@gmail.com')
            ->view('emails.receipt');
    }
}

The second function is:

public function getUserReceipts(Request $data){
    $email = $data->email;

    $donor = Donor::where('email', $email)->get(); 

   return view('emails.receipt')->with(compact('donor'));
}

The receipt file simply contains:

@foreach($donor as $value)
    
    
    
@endforeach

The error I'm getting seems to be because donor in the receipt file is undefined and I'm not sure how to fix it as it was passed with compact in the second function.

Would really appreciate the help.



via AJth

Advertisement