Thursday, March 2, 2017

pass var from controller to mail class laravel

I am trying to send a mail with a attachment. Its based on a var from my controller. But I don't know how to pass the content of the variable from my controller to my mail class. My mail view has no problem with using the vars. I need the var in the build method to pick the right file from my directory I generated in my controller.

My controller store method

$aanvraag = new Aanvragen;

$aanvraag->naam = request('naam');
$aanvraag->naam = request('email');
$aanvraag->save();

$data = array (

    array('Name', 'Email'),
    array($aanvraag->naam, $aanvraag->email)

);

\Excel::create($aanvraag->naam.'sheet', function ($excel) use ($data) {
    $excel->sheet('Sheet1', function ($sheet) use ($data) {
        $sheet->fromArray($data);
    });
})->save();

\Mail::to('johndoe@info.com')->send(new Aanvraag($aanvraag));

return redirect('/');

My mail class

class Aanvraag extends Mailable
{
    use Queueable, SerializesModels;

    public $aanvraag;

    public function __construct($aanvraag)
    {
        $this->aanvraag = $aanvraag;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build($aanvraag)
    {

        return $this->view('mails.aanvraag')->attach(url('/exports/'. $aanvraag .'.xls'));

    }
}

This returns the following

Missing argument 1 for App\Mail\Aanvraag::build()



via Gijsberts

Advertisement