0

My controller in the with the id that is the only thing that I pass in the request: { "id": 1} I look for the matches in my view and then in another table that uses that same id but it gives me an undefined variable error:

    public function store(Request $request){
        $calculo = TotalPedido::where('id', $request->id)->with('productos')->get();
        $transferencia = Transferencias::where('id', $request->id)->with('visitador','cliente')->get();
        $email = new GananciaEstimada($calculo, $transferencia);

        //$vistador = Transferencias::with('visitador')->get()->find([$id]);
        
        Mail::to('[email protected]')->send($email);
    }
}

My maileable:

class GananciaEstimada extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mails.estimadas', [
            'calculo' => $calculo->total,
            'transferencia' => $transferencia,
        ]);
    }
}

Error:

ErrorException: Undefined variable: calculo in file C:\Users\andry\Documents\Programacion\Transferencias-Backend\app\Mail\GananciaEstimada.php on line 37

1 respuesta 1

0

Esto se debe a un error en el scope o 'ámbito' de tus variables. Dentro de tu método build estas llamando a $calculo->total, pero $calculo no está definido ni como parametro ni como variable de tu función. Entiendo que quieres hacer referencia a la propiedad pública $calculo, pero debes acceder a ella con $this->:

public function build()
  {
      return $this->view('mails.estimadas', [
          'calculo' => $this->calculo->total,
          'transferencia' => $this->transferencia,
      ]);
    }

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.