From 7223f122417f44490ddbf4519aa9cea3f762999e Mon Sep 17 00:00:00 2001 From: Reza Date: Wed, 7 Aug 2019 16:19:39 +0430 Subject: [PATCH 1/3] set db engine "InnoDB" for some tables --- .../2014_12_02_152920_create_password_reminders_table.php | 1 + .../migrations/2016_02_10_181651_create_roles_tables.php | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php b/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php index dfbcf83..fe98e4f 100644 --- a/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php +++ b/src/database/migrations/2014_12_02_152920_create_password_reminders_table.php @@ -17,6 +17,7 @@ public function up() $table->string('email')->index(); $table->string('token')->index(); $table->timestamp('created_at'); + $table->engine = 'InnoDB'; }); } diff --git a/src/database/migrations/2016_02_10_181651_create_roles_tables.php b/src/database/migrations/2016_02_10_181651_create_roles_tables.php index fb4c5dc..f74f418 100644 --- a/src/database/migrations/2016_02_10_181651_create_roles_tables.php +++ b/src/database/migrations/2016_02_10_181651_create_roles_tables.php @@ -17,6 +17,7 @@ public function up() $table->string('name'); $table->string('label')->nullable(); $table->timestamps(); + $table->engine = 'InnoDB'; }); Schema::create('permissions', function (Blueprint $table) { @@ -24,11 +25,13 @@ public function up() $table->string('name'); $table->string('label')->nullable(); $table->timestamps(); + $table->engine = 'InnoDB'; }); Schema::create('permission_role', function (Blueprint $table) { $table->integer('permission_id')->unsigned(); $table->integer('role_id')->unsigned(); + $table->engine = 'InnoDB'; $table->foreign('permission_id') ->references('id') @@ -46,6 +49,7 @@ public function up() Schema::create('admin_role', function (Blueprint $table) { $table->integer('role_id')->unsigned(); $table->integer('admin_id')->unsigned(); + $table->engine = 'InnoDB'; $table->foreign('role_id') ->references('id') From 578d408dd7093339ddf806faeabab2e7c36db8c0 Mon Sep 17 00:00:00 2001 From: Reza Date: Sun, 1 Mar 2020 11:30:00 +0330 Subject: [PATCH 2/3] add table prefix for Import method --- src/controllers/ExportImportController.php | 63 +++++++++++----------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/controllers/ExportImportController.php b/src/controllers/ExportImportController.php index d42510f..d4c50c0 100755 --- a/src/controllers/ExportImportController.php +++ b/src/controllers/ExportImportController.php @@ -29,37 +29,38 @@ public function import($entity) { $appHelper = new libs\AppHelper(); - $className = $appHelper->getModel($entity); - $model = new $className; - $table = $model->getTable(); - $columns = \Schema::getColumnListing($table); - $key = $model->getKeyName(); - - $notNullColumnNames = array(); - $notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $table . "` where `Null` = 'no'")); - if (!empty($notNullColumnsList)) { - foreach ($notNullColumnsList as $notNullColumn) { - $notNullColumnNames[] = $notNullColumn->Field; - } - } - - $status = Input::get('status'); - - $filePath = null; - if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) { - $filePath = Input::file('import_file')->getRealPath(); - } - - if ($filePath) { - - \Excel::load($filePath, function($reader) use ($model, $columns, $key, $status, $notNullColumnNames) { - $this->importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames); - }); - } - - $importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess'); - - return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage); + $className = $appHelper->getModel($entity); + $model = new $className; + $tablePrefix = \DB::getTablePrefix(); + $table = $tablePrefix.$model->getTable(); + $columns = \Schema::getColumnListing($table); + $key = $model->getKeyName(); + + $notNullColumnNames = array(); + $notNullColumnsList = \DB::select(\DB::raw("SHOW COLUMNS FROM `" . $table . "` where `Null` = 'no'")); + if (!empty($notNullColumnsList)) { + foreach ($notNullColumnsList as $notNullColumn) { + $notNullColumnNames[] = $notNullColumn->Field; + } + } + + $status = Input::get('status'); + + $filePath = null; + if (Input::hasFile('import_file') && Input::file('import_file')->isValid()) { + $filePath = Input::file('import_file')->getRealPath(); + } + + if ($filePath) { + + \Excel::load($filePath, function($reader) use ($model, $columns, $key, $status, $notNullColumnNames) { + $this->importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames); + }); + } + + $importMessage = ($this->failed == true) ? \Lang::get('panel::fields.importDataFailure') : \Lang::get('panel::fields.importDataSuccess'); + + return \Redirect::to('panel/' . $entity . '/all')->with('import_message', $importMessage); } public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) { From b8170d66310e5469606723291a7cc4d7ad166e7c Mon Sep 17 00:00:00 2001 From: Reza Date: Sun, 1 Mar 2020 12:41:54 +0330 Subject: [PATCH 3/3] Upgrade export function to new version of "Maatwebsite\Excel" --- src/controllers/ExportImportController.php | 19 +++++----------- src/models/EntityExport.php | 26 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 src/models/EntityExport.php diff --git a/src/controllers/ExportImportController.php b/src/controllers/ExportImportController.php index d4c50c0..2977584 100755 --- a/src/controllers/ExportImportController.php +++ b/src/controllers/ExportImportController.php @@ -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) { diff --git a/src/models/EntityExport.php b/src/models/EntityExport.php new file mode 100644 index 0000000..f0166ba --- /dev/null +++ b/src/models/EntityExport.php @@ -0,0 +1,26 @@ +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; + } +} \ No newline at end of file