Home > Blockchain >  Laravel 9 - ErrorException: Undefined variable $order_id while sending mail
Laravel 9 - ErrorException: Undefined variable $order_id while sending mail

Time:11-05

Hello in my laravel application i have a moment when the user is notified on mail when tha order is complete.

` In my controller i have:

Mail::to('test@mail')->send(new OrderSuccess($id));

// $id is a string

in mail.php

<?php

namespace App\Mail;

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

class OrderSuccess extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $order_id;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mails.ordermail')
                    ->with([
                        'order_id' => $this->order_id
                    ]);
    }
}

In mail blade template:

Test order n. {{ $order_id }}

But i'm getting a failed job that shows this error

ErrorException: Undefined variable $order_id in C:\Users...

What am i doing wrong?

`

CodePudding user response:

Your code looks correct. Try to restart your queue worker.

Queue workers are long-lived processes and store the booted application state in memory.

CodePudding user response:

Try with below given code.

return $this->view('mails.ordermail',['order_id' => $this->order_id]);
  • Related