diff --git a/src/lara-crud/Console/Controller.php b/src/lara-crud/Console/Controller.php index 55fdd6a..66ebeaf 100755 --- a/src/lara-crud/Console/Controller.php +++ b/src/lara-crud/Console/Controller.php @@ -55,6 +55,7 @@ public function handle() if (class_exists($modelFullName)) { $modelObj = new $modelFullName; $requestResource = new RequestResourceCrud($modelObj->getTable(), false, $api); + $requestResource->setModel($modelFullName); $requestResource->save(); $this->info('Request controller classes created successfully'); } diff --git a/src/lara-crud/Console/Factory.php b/src/lara-crud/Console/Factory.php index e696775..01c36e4 100644 --- a/src/lara-crud/Console/Factory.php +++ b/src/lara-crud/Console/Factory.php @@ -3,6 +3,7 @@ namespace LaraCrud\Console; use Illuminate\Console\Command; +use LaraCrud\Crud\ModelFactory; class Factory extends Command { @@ -31,7 +32,7 @@ public function handle() $model = $this->argument('model'); $name = $this->option('name'); - $factoryCrud = new \LaraCrud\Crud\ModelFactory($model, $name); + $factoryCrud = new ModelFactory($model, $name); $factoryCrud->save(); $this->info('Factory class created successfully'); } catch (\Exception $ex) { diff --git a/src/lara-crud/Crud/ModelFactory.php b/src/lara-crud/Crud/ModelFactory.php index 20ac43c..9e5c325 100644 --- a/src/lara-crud/Crud/ModelFactory.php +++ b/src/lara-crud/Crud/ModelFactory.php @@ -11,6 +11,7 @@ use DbReader\Table; use Illuminate\Database\Eloquent\Model; use LaraCrud\Contracts\Crud; +use LaraCrud\Helpers\FakerColumn; use LaraCrud\Helpers\Helper; use LaraCrud\Helpers\TemplateManager; @@ -85,7 +86,10 @@ protected function makeColumns() if ($column->isProtected()) { continue; } - $arr .= "\t\t" . '"' . $column->name() . '"=>\'\',' . PHP_EOL; + $fakerColumn = new FakerColumn($column); + $default = $fakerColumn->default(); + $columnValue = !empty($default) ? $default.',' : '\'\','; + $arr .= "\t\t" . '"' . $column->name() . '" => ' . $columnValue . PHP_EOL; }; return $arr; } diff --git a/src/lara-crud/Helpers/FakerColumn.php b/src/lara-crud/Helpers/FakerColumn.php index 9f4c596..c286ee8 100644 --- a/src/lara-crud/Helpers/FakerColumn.php +++ b/src/lara-crud/Helpers/FakerColumn.php @@ -13,7 +13,82 @@ class FakerColumn { - protected $map = []; + protected $map = [ + 'firstName' => [ + 'first_name', + 'first_name', + 'fname' + ], + 'lastName' => [ + 'last_name', + 'lastname', + 'lname' + ], + 'name' => [ + 'full_name', + 'name' + ], + 'safeEmail' => [ + 'email', + 'email_address', + 'emailaddress' + ], + 'address' => [ + 'address', + 'street_address', + 'street', + ], + 'city' => [ + 'city', + 'suburb', + 'locality', + 'state', + 'village', + 'town' + ], + 'country' => [ + 'country', + ], + 'realText()' => [ + 'title', + 'subject', + 'message', + 'reply', + 'comments', + 'comment', + 'feedback', + 'body', + 'content', + 'description', + 'about', + 'profile' + ], + 'slug' => [ + 'slug' + ], + 'phoneNumber' => [ + 'phone', + 'phone_number', + 'mobile', + 'cell', + 'mobile_number', + 'cell_number', + 'telephone', + 'personal_number', + 'business_number', + 'emergency_cell', + 'emergency_phone', + ], + 'imageUrl()' => [ + 'avatar', + 'photo', + 'image', + 'image_url', + 'document', + 'file' + ] + + ]; /** * @var Column */ @@ -29,47 +104,57 @@ public function get() } - protected function default() + public function default() { switch ($this->column->type()) { case 'varchar': + foreach ($this->map as $faker => $columns) { + if (in_array($this->column->name(), $columns)) { + return '$faker->' . $faker; + } + } break; case 'enum': + return 'array_rand([\'' . implode("','", $this->column->options()) . '\'], 1)'; break; - case 'longText': + case 'longtext': case 'mediumtext': case 'text': case 'tinytext': + if (in_array($this->column->name(), $this->map['realText()'])) { + return '$faker->realText()'; + } else { + return '$faker->text'; + } break; // Numeric data Type case 'bigint': - break; case 'mediumint': - break; case 'int': + return '$faker->randomNumber()'; break; case 'smallint': - break; case 'tinyint': + return '$faker->numberBetween(1,99)'; break; case 'decimal': - break; case 'float': - break; case 'double': + return '$faker->randomNumber()'; break; // Date Time case 'date': + return '$faker->date()'; break; case 'datetime': + case 'timestamp': + return '$faker->dateTime()'; break; case 'time': + return '$faker->time()'; case 'year': + return '$faker->year'; break; - break; - case 'timestamp': - break; - } } } \ No newline at end of file diff --git a/src/lara-crud/LaraCrudServiceProvider.php b/src/lara-crud/LaraCrudServiceProvider.php index a7e93d9..ba8849e 100755 --- a/src/lara-crud/LaraCrudServiceProvider.php +++ b/src/lara-crud/LaraCrudServiceProvider.php @@ -6,6 +6,7 @@ use DbReader\Database; use Illuminate\Support\ServiceProvider; use LaraCrud\Console\Controller; +use LaraCrud\Console\ControllerMethod; use LaraCrud\Console\Factory; use LaraCrud\Console\Migration; use LaraCrud\Console\Model;