Laravel Mail, Error: Undefined variable

Asked

Viewed 230 times

0

Good evening friends,

I’m having a problem, when I try to pass the variables from one control to the other, he ends up accusing that the variable "was not defined":

(Undefined variable: Black and white)

I don’t know what I’m doing wrong, could anyone help me? I need a light rsrs...


Schedulecontroller.php

use App\Schedule;
use App\Mail\AgendamentoEpcSup;
use Illuminate\Support\Facades\Mail;

public function store(Request $request)
        {
            $schedule = New Schedule();
            $schedule->user_id = $request -> input('user_id');
            $schedule->room_id = $request -> input('room_id');
            $data_str = \DateTime::createFromFormat('d-m-Y', $request-> input ('date_choice'));
            $schedule->date = $data_str->format('Y-m-d');
            $schedule->hour_in = $request-> input('hour_in');
            $schedule->hour_out = $request-> input('hour_out');

            $schedule->save();

            $user = Auth::user();
            $user_email = Auth::user()->email;

            Mail::to($user_email)->send(new AgendamentoEpcSup($schedule, $user));

            return redirect()->route('schedules.index')->with('status', 'Novo agendamento criado, lembrando que cancelamentos só podem ser feitos com 12h de antecedência pelo sistema.');
        }

Scheduling epcsup.php

namespace App\Mail;

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

class AgendamentoEpcSup extends Mailable
{
    use Queueable, SerializesModels;

    public $schedule;
    public $user;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.agendamento', compact('schedule', 'user'));
    }
}

I don’t know why the values aren’t being passed :/

Thank you so much!

1 answer

3


The problem:

In AgendamentoEpcSup.php, in the __construct() you do:

$this->schedule = $schedule

...but did not set $Schedule anywhere, the error is only stating the obvious. How did the __construct will know what it is $schedule?

If you want to initialize variables in the constructor you need something like this:

public function __construct($var1, $var2) // <-- Atenção aqui! Nós acrescentamos
                                          // duas variáveis para receber o que
                                          // foi enviado pelo new.
{
    $this->schedule = $var1;              // Agora sim, estamos usando algo que existe
    $this->user = $var2;
}

Which is to match the call:

AgendamentoEpcSup($schedule, $user)

Note: I used $var1 and $var2 purposely, for you to see that the names within the __construct() regardless of the names used in the call, but feel free to use the names as you wish.

  • 1

    Noooooossa I deserved HAHAHAHAHHAHAHA... Worse than I have reviewed it 10 times and for me it was ok... Tired mind is complicated... But thank you for the help... Possibly I would be questioning me so far rsrs... In my head, as I had already stated public $Schedule;, I was thinking that the $this-> was already pulling the values HAHAHHAHAHA... Wow, really worth...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.