Laravel Mail, Error: Variable undefined

0

Good evening, friends,

I'm having a problem, when I try to pass the variables from one control to another, it ends up accusing the variable "not defined":

(Undefined variable: schedule)

I do not 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.');
        }

SchedulingEpcSup.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 do not know why values are not being passed: /

Thank you in advance!

    
asked by anonymous 21.09.2018 / 05:43

1 answer

3

The problem:

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

$this->schedule = $schedule

... but did not set $ schedule anywhere, the error is only informing the obvious. How will __construct know what $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;
}

What is to match the call:

AgendamentoEpcSup($schedule, $user)

Note: I've used $var1 and $var2 purposely, so you'll see that the names within __construct() are independent of the names used in the call, but feel free to use the names however you want.

    
21.09.2018 / 05:52