Reputation: 612
I want to download 2 user's data from tables, I want to generate excel file of that data.
here is the download function for only one data table called registerdetails.
public function export(){
$items = registerdetails::all();
Excel::create('General Trainee Details', function($excel) use($items){
$excel->sheet('Sheet 1', function($sheet) use($items){
$sheet->fromArray($items);
});
})->export('xlsx');
}
I need this controller to be modified get datas from registerdetails and bankdetails. if anyone can help me to get this solved?
Upvotes: 0
Views: 1086
Reputation: 27503
try this
public function collectionexport_all(){
$items = registerdetails::join('bankdetails', 'bankdetails.id', '=', 'registerdetails.id')->get();
$itemsArray = [];
foreach ($items as $item) {
$itemsArray[] = $item->toArray();
}
Excel::create('Full Details', function($excel) use($itemsArray){
$excel->sheet('Sheet 1', function($sheet) use($itemsArray){
$sheet->fromArray($itemsArray);
});
})->export('xlsx');
}
Upvotes: 1
Reputation: 555
I'm not sure what you want to achieve.. But I hope this helps.
The Excel Library you're using (hoping it's maatwebsite/excel or its like) can make use of Eloquent queries or Query Builder like so:
public function export($id){
Excel::create('General Trainee Details', function($excel) use($id){
$excel->sheet('Sheet 1', function($sheet) use($id){
$query= registerdetails::select(['registerdetailscolumn_1', 'registerdetailscolumn_2')->leftjoin('bankdetails', 'registerdetailscolumn_id', '=', 'bankdetailscolumn_id')->where(registerdetailscolumn_id, '=', $id);
$sheet->fromModel($query);
});
})->export('xlsx');
}
Of course, knowing you can provide and pass along whatever employee's id it is.
you can export one table on one sheet and another table on another sheet like so:
public function export(){
Excel::create('General Trainee Details', function($excel) use($id){
$excel->sheet('Sheet 1', function($sheet){
$query= registerdetails::all();
$sheet->fromModel($query);
});
$excel->sheet('Sheet 2', function($sheet){
$query= bankdetails::all();
$sheet->fromModel($query);
});
})->export('xlsx');
}
Upvotes: 0