From 1b7f2071aa523b92c84b03fe253937766829e1a7 Mon Sep 17 00:00:00 2001 From: Antonio Almeida Date: Sat, 11 Dec 2021 01:02:57 +0000 Subject: [PATCH] Add support to mongodb driver --- .../CrudControllerBackpackCommand.php | 11 +++++++++- .../Commands/CrudModelBackpackCommand.php | 20 ++++++++++++++++++- src/Console/Commands/ModelBackpackCommand.php | 20 ++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/CrudControllerBackpackCommand.php b/src/Console/Commands/CrudControllerBackpackCommand.php index 07f2860..58d1506 100644 --- a/src/Console/Commands/CrudControllerBackpackCommand.php +++ b/src/Console/Commands/CrudControllerBackpackCommand.php @@ -101,8 +101,17 @@ protected function getAttributes($model) if (count($model->getFillable())) { $attributes = $model->getFillable(); } else { + $driver = \Schema::getConnection()->getConfig('driver'); + // otherwise, if guarded is used, just pick up the columns straight from the bd table - $attributes = \Schema::getColumnListing($model->getTable()); + switch ($driver) { + case 'mongodb': + $attributes = []; + break; + default: + $attributes = \Schema::getColumnListing($model->getTable()); + break; + } } return $attributes; diff --git a/src/Console/Commands/CrudModelBackpackCommand.php b/src/Console/Commands/CrudModelBackpackCommand.php index 1106939..96f3451 100644 --- a/src/Console/Commands/CrudModelBackpackCommand.php +++ b/src/Console/Commands/CrudModelBackpackCommand.php @@ -162,6 +162,24 @@ protected function replaceTable(&$stub, $name) return $this; } + /** + * Replace Model on MongoDB. + * + * @param string $stub + * @param string $name + * @return string + */ + protected function replaceDriverDB(&$stub) + { + $driver = \Schema::getConnection()->getConfig('driver'); + + if ($driver === 'mongodb') { + $stub = str_replace(\Illuminate\Database\Eloquent\Model::class, \Jenssegers\Mongodb\Eloquent\Model::class, $stub); + } + + return $this; + } + /** * Build the class with the given name. * @@ -172,7 +190,7 @@ protected function buildClass($name) { $stub = $this->files->get($this->getStub()); - return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name); + return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceDriverDB($stub)->replaceClass($stub, $name); } /** diff --git a/src/Console/Commands/ModelBackpackCommand.php b/src/Console/Commands/ModelBackpackCommand.php index a94f6b9..b365bbc 100644 --- a/src/Console/Commands/ModelBackpackCommand.php +++ b/src/Console/Commands/ModelBackpackCommand.php @@ -78,6 +78,24 @@ protected function replaceTable(&$stub, $name) return $this; } + /** + * Replace Model on MongoDB. + * + * @param string $stub + * @param string $name + * @return string + */ + protected function replaceDriverDB(&$stub) + { + $driver = \Schema::getConnection()->getConfig('driver'); + + if ($driver === 'mongodb') { + $stub = str_replace(\Illuminate\Database\Eloquent\Model::class, \Jenssegers\Mongodb\Eloquent\Model::class, $stub); + } + + return $this; + } + /** * Build the class with the given name. * @@ -88,7 +106,7 @@ protected function buildClass($name) { $stub = $this->files->get($this->getStub()); - return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name); + return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceDriverDB($stub)->replaceClass($stub, $name); } /**