0

I had this working, using Laravel 4 and Laravel Excel to export data from mySQL. I have objects for Regtype and Attendee and I want one sheet per regtype with all related attendees. I had a single sheet of attendees exporting great. Now I've added a loop for the regtypes and I'm able to get multiple sheets but all sheets are blank!

Perhaps I have to use shareView? I wouldn't think so.. I get no errors and none of the data I pass to the blade template is displaying.

My export function: public function export() { $date = date('Y_m_d'); \Excel::create('CMO_Connect_Attendees_Export_'.$date, function($excel) { $regtypes = Regtype::all(); foreach ($regtypes as $regtype) { if( $regtype->attendees(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');
    });
}

And the blade template I'm passing data to (I pass the attendees and also the date and name of the regtype to display at the top of the sheet:

<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>

Thanks for any help!

Dylan Glockler
  • 1,115
  • 1
  • 20
  • 40

1 Answers1

0

I sorted out - below is my export function now. I updated the vendor files through composer (realized I was on a slightly older version) and revised my code with use($regtype when calling the $sheet method (passing the regtype object into the sheet). I assumed that you'd pass variables or objects into the sheet the same way you pass data into the blade template but that's not correct.

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');
    });
}
Dylan Glockler
  • 1,115
  • 1
  • 20
  • 40