|
11 | 11 | class FillModel extends Command |
12 | 12 | { |
13 | 13 |
|
14 | | - private const EXCLUDE_FIELDS = ['id','created_at','updated_at']; |
| 14 | + private const EXCLUDE_FIELDS = ['id', 'created_at', 'updated_at']; |
15 | 15 |
|
16 | 16 | /** |
17 | 17 | * The name and signature of the console command. |
18 | 18 | * |
19 | 19 | * @var string |
20 | 20 | */ |
21 | | - protected $signature = 'commands:model:fill {name}'; |
| 21 | + protected $signature = 'cmd:model:fill {model}'; |
22 | 22 |
|
23 | 23 | /** |
24 | 24 | * The console command description. |
25 | 25 | * |
26 | 26 | * @var string |
27 | 27 | */ |
28 | | - protected $description = 'Fill the model'; |
| 28 | + protected $description = 'Fill model'; |
29 | 29 |
|
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(){ |
42 | 30 |
|
43 | | - $argument = $this->argument('name'); |
| 31 | + public function handle() |
| 32 | + { |
44 | 33 |
|
45 | | - $parts = explode("/",$argument); |
| 34 | + $argument = $this->argument('model'); |
46 | 35 |
|
47 | | - $model = $this->getNamespace() ; |
| 36 | + $parts = explode("/", $argument); |
48 | 37 |
|
49 | 38 |
|
| 39 | + $model = $this->getNamespace(); |
50 | 40 |
|
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 | + } |
53 | 47 | } |
54 | 48 |
|
| 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 | + } |
55 | 56 |
|
56 | 57 | $table_name = $this->getTableName(end($parts)); |
57 | 58 |
|
58 | | - |
59 | | - |
60 | 59 | $table_fields = $this->getTableFields($table_name); |
61 | 60 |
|
62 | | - new A; |
63 | | - |
64 | | - dd($model, $table_name, $table_fields); |
65 | | - |
66 | | - $fields = []; |
| 61 | + $model = new $model; |
67 | 62 |
|
68 | 63 | 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 | + } |
70 | 69 | } |
71 | 70 |
|
| 71 | + $model->save(); |
72 | 72 |
|
73 | | - $model::create($fields); |
74 | | - |
75 | | - $this->info('Model filled succesfuly'); |
76 | 73 |
|
77 | | - // dd($model); |
| 74 | + $this->info("Model filled succesfuly"); |
78 | 75 | } |
79 | 76 |
|
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 | + { |
81 | 84 | return strtolower(Str::plural($name)); |
82 | 85 | } |
83 | 86 |
|
84 | | - private function getModelName() :string{ |
| 87 | + /** |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + private function getModelName(): string |
| 91 | + { |
85 | 92 | return ucfirst($this->argument('name')); |
86 | 93 | } |
87 | 94 |
|
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 | + { |
89 | 101 | $table_fields = Schema::getColumnListing($table_name); |
90 | 102 |
|
91 | | - return array_diff($table_fields,self::EXCLUDE_FIELDS); |
92 | | - |
| 103 | + return array_diff($table_fields, self::EXCLUDE_FIELDS); |
93 | 104 | } |
94 | 105 |
|
95 | | - /** |
| 106 | + /** |
96 | 107 | * Get project namespace |
97 | 108 | * Default: App |
98 | 109 | * @return string |
|
0 commit comments