我认为我已经接近成功了,我只需要将$excelFile传递给Mail,但它一直显示未定义,但是当我传递$truckstop_post时,它可以正常通过,但这些数据格式不正确,因为它还没有经过Excel::create。如何将\Excel::create的结果传递到Mail::send中?
public function truckstopPost()
{
$type = 'csv';
$truckstop_post = Loadlist::select('pick_city', 'pick_state', 'delivery_city', 'delivery_state', 'trailer_type', 'pick_date', 'load_type', 'length', 'width', 'height', 'weight', 'offer_money', 'special_instructions', 'company_contact', 'contact_phone')->where('urgency', 'OPEN')->orderBy('id', 'desc')->get();
$excelFile = \Excel::create('itransys', function($excel) use ($truckstop_post) {
$excel->sheet('mySheet', function($sheet) use ($truckstop_post)
{
$sheet->fromArray($truckstop_post);
});
$info = Load::find(8500);
$info = ['info'=>$info];
Mail::send(['html'=>'email.invoice_email_body'], $info, function($message) use ($info, $excelFile){
$message->to('mike@gmail.com')->subject('subject');
$message->from('mike@gmail.com', \Auth::user()->name);
$message->attachData($excelFile, 'Invoice.csv');
});
});
return back()->with('status', 'You Posted Truckstop!');
}
如果我将$truckstop_post传递给attachData(),结果看起来就像这样,但是这不是一个格式良好的csv文件。