Skip to content

Commit b4c0900

Browse files
committed
update fill model command
1 parent 495e863 commit b4c0900

File tree

13 files changed

+57
-781
lines changed

13 files changed

+57
-781
lines changed

config/command.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44
/*
55
* The admin model namespace
66
*/
7-
'admin_model' => 'App\\Admin' # App\Admin::class
7+
'admin_model' => 'App\\Admin', # App\Admin::class
8+
9+
/**
10+
* Le dossier ou mettre les models dans le dossier app
11+
* Laisser vide si vous voulez les mettre à la racine
12+
*/
13+
// 'models_folder' => 'Models',
14+
'models_folder' => '',
815
];

src/Console/Commands/Database/FillModel.php

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,88 +11,99 @@
1111
class FillModel extends Command
1212
{
1313

14-
private const EXCLUDE_FIELDS = ['id','created_at','updated_at'];
14+
private const EXCLUDE_FIELDS = ['id', 'created_at', 'updated_at'];
1515

1616
/**
1717
* The name and signature of the console command.
1818
*
1919
* @var string
2020
*/
21-
protected $signature = 'commands:model:fill {name}';
21+
protected $signature = 'cmd:model:fill {model}';
2222

2323
/**
2424
* The console command description.
2525
*
2626
* @var string
2727
*/
28-
protected $description = 'Fill the model';
28+
protected $description = 'Fill model';
2929

30-
/**
31-
* Create a new command instance.
32-
*
33-
* @return void
34-
*/
35-
public function __construct()
36-
{
37-
parent::__construct();
38-
}
39-
40-
41-
public function handle(){
4230

43-
$argument = $this->argument('name');
31+
public function handle()
32+
{
4433

45-
$parts = explode("/",$argument);
34+
$argument = $this->argument('model');
4635

47-
$model = $this->getNamespace() ;
36+
$parts = explode("/", $argument);
4837

4938

39+
$model = $this->getNamespace();
5040

51-
foreach ($parts as $value) {
52-
$model .= '\\' . $value;
41+
if (1 === count($parts) && !empty($model_folder = config('command.models_folder', ''))) {
42+
$model .= sprintf("\%s\%s", $model_folder, ucfirst(end($parts)));
43+
} else {
44+
foreach ($parts as $value) {
45+
$model .= '\\' . ucfirst($value);
46+
}
5347
}
5448

49+
// check if model exists
50+
if (!class_exists($model)) {
51+
$this->error(
52+
sprintf("The [%s] model does not exists", $model)
53+
);
54+
return;
55+
}
5556

5657
$table_name = $this->getTableName(end($parts));
5758

58-
59-
6059
$table_fields = $this->getTableFields($table_name);
6160

62-
new A;
63-
64-
dd($model, $table_name, $table_fields);
65-
66-
$fields = [];
61+
$model = new $model;
6762

6863
foreach ($table_fields as $field) {
69-
$fields[$field] = $this->ask('Enter ' . $field);
64+
if ($field === 'password') {
65+
$model->$field = $this->secret('Enter ' . $field);
66+
} else {
67+
$model->$field = $this->ask('Enter ' . $field);
68+
}
7069
}
7170

71+
$model->save();
7272

73-
$model::create($fields);
74-
75-
$this->info('Model filled succesfuly');
7673

77-
// dd($model);
74+
$this->info("Model filled succesfuly");
7875
}
7976

80-
private function getTableName(string $name) :string{
77+
78+
/**
79+
* @param string $name
80+
* @return string
81+
*/
82+
private function getTableName(string $name): string
83+
{
8184
return strtolower(Str::plural($name));
8285
}
8386

84-
private function getModelName() :string{
87+
/**
88+
* @return string
89+
*/
90+
private function getModelName(): string
91+
{
8592
return ucfirst($this->argument('name'));
8693
}
8794

88-
private function getTableFields(string $table_name) :array {
95+
/**
96+
* @param string $table_name
97+
* @return array
98+
*/
99+
private function getTableFields(string $table_name): array
100+
{
89101
$table_fields = Schema::getColumnListing($table_name);
90102

91-
return array_diff($table_fields,self::EXCLUDE_FIELDS);
92-
103+
return array_diff($table_fields, self::EXCLUDE_FIELDS);
93104
}
94105

95-
/**
106+
/**
96107
* Get project namespace
97108
* Default: App
98109
* @return string

src/Console/Commands/Entity/BaseEntity.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Console/Commands/Entity/CreateEntity.php

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)