Skip to content

Commit

Permalink
Upgrade export function to new version of "Maatwebsite\Excel"
Browse files Browse the repository at this point in the history
  • Loading branch information
RezaAb committed Mar 1, 2020
1 parent 578d408 commit b8170d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/controllers/ExportImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,18 @@

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
use Maatwebsite\Excel\Facades\Excel;

class ExportImportController extends Controller {

protected $failed = false;

public function export($entity, $fileType) {

$appHelper = new libs\AppHelper();

$className = $appHelper->getModel($entity);
$data = $className::get();
if (strcmp($fileType, "excel") == 0) {
$excel = \App::make('Excel');
\Excel::create($entity, function($excel) use ($data) {
$excel->sheet('Sheet1', function($sheet) use ($data) {
$sheet->fromModel($data);
});
})->export('xls');
}
if (strcmp($fileType, "excel") == 0) {
$export = new EntityExport($entity);
return Excel::download($export, $entity.'.xlsx');
}
return \Redirect::to('panel/' . $entity . '/all')->with('export_message', "File type is not excel");
}

public function import($entity) {
Expand Down
26 changes: 26 additions & 0 deletions src/models/EntityExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Serverfireteam\Panel;

use Maatwebsite\Excel\Concerns\FromArray;
use Serverfireteam\Panel\libs\AppHelper;

class EntityExport implements FromArray
{
protected $entity;

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

public function array(): array
{
$appHelper = new libs\AppHelper();
$className = $appHelper->getModel($this->entity);
$data = $className::all()->ToArray();
$data= json_decode( json_encode($data), true);
return $data;
}
}

0 comments on commit b8170d6

Please sign in to comment.