我有这个工作,使用Laravel 4和Laravel Excel从mySQL导出数据。我有Regtype和Attendee的对象,我希望每个regtype有一张表与所有相关的与会者。我有一张出口好的参加者。现在我为regtypes添加了一个循环,我可以获得多张纸,但所有纸张都是空白的!
也许我必须使用shareView?我不这么认为。我没有错误,我传递给刀片模板的数据都没有显示。
我的导出功能: 公共功能出口() { $ date = date(' Y_m_d'); \ Excel :: create(' CMO_Connect_Attendees_Export _'。$ date,function($ excel){ $ regtypes = Regtype :: all(); foreach($ regtypes as $ regtype){ if($ regtype->参与者(3) - > count()){
$excel->sheet($regtype->name, array('regtype' => $regtype), function($sheet) {
$date = date('Y_m_d');
$attendees = new Attendee;
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export', array('atts' => $atts))->with('curdate',$date)->with('regtype_name',$regtype->name);
$atts = '';
});
} //endif
} //endforeach
$excel->export('xls');
});
}
刀片模板我将数据传递给(我传递了与会者以及注册表的日期和名称,以显示在工作表的顶部:
<html>
<table>
<tr>
<td colspan="11">Attendees Export - {{ $curdate }} - {{ $regtype_name }}</td>
</tr>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Company</td>
<td>Title</td>
<td>Email</td>
<td>Phone</td>
<td>Address</td>
<td>Addres 2</td>
<td>City</td>
<td>State</td>
<td>Zip</td>
<td>Date Created</td>
</tr>
@foreach($atts as $att)
<tr>
<td> {{ $att->firstname }} </td>
<td> {{ $att->lastname }} </td>
<td> {{ $att->company }} </td>
<td> {{ $att->title }} </td>
<td> {{ $att->email }} </td>
<td> {{ $att->phone }} </td>
<td> {{ $att->address }} </td>
<td> {{ $att->address2 }} </td>
<td> {{ $att->city }} </td>
<td> {{ $att->state }} </td>
<td> {{ $att->zip }} </td>
<td> {{ $att->created_at }} </td>
</tr>
@endforeach
</table>
</html>
感谢您的帮助!
答案 0 :(得分:0)
我整理出来 - 下面是我的导出功能。我通过composer更新了供应商文件(意识到我的版本稍微有些旧版本)并在调用use($regtype
方法时将$sheet
修改为代码(将regtype对象传递到工作表中)。我假设您将变量或对象传递到工作表中的方式与将数据传递到刀片模板的方式相同,但这不正确。
public function export()
{
$date = date('Y_m_d');
\Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) {
$excel->setTitle('CMO Connect Attendee Data Export');
$excel->setCreator('Dylan Glockler')
->setCompany('Bryan Allen Events');
$excel->setDescription('All attendee event data for Adobe CMO Connect');
$regtypes = Regtype::all();
$summary = '';
foreach ($regtypes as $regtype) {
if( $regtype->attendees(3)->count() ) {
$summary = $summary . $regtype->name;
$summary = $summary . ': '.$regtype->attendees(3)->count();
$summary = $summary . ' | ' . $regtype->id . '<br />';
$excel->sheet($regtype->name, function($sheet) use($regtype) {
$date = date('Y_m_d');
$atts = Attendee::where('block_id', '=', \Input::get('block_id'))
->where('regtype_id', '=', $regtype->id)
->get();
$sheet->setStyle(array(
'font' => array(
'name' => 'Arial',
'size' => 12,
'bold' => false
)
));
$sheet->loadView('attendees.export')->with('curdate',$date)->with('atts',$atts)->with('regtype_name',$regtype->name)->with('att_count',$regtype->attendees(3)->count());
});
} //endif
} //endforeach
$excel->export('xls');
});
}