Skip to content

Commit ef496d3

Browse files
authored
Merge pull request #104 from Laravel-Backpack/fix-95
Fixed wrong condition causing fields and columns to not be filled
2 parents c95760b + cc27219 commit ef496d3

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/Console/Commands/CrudControllerBackpackCommand.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function getAttributes($model)
9696
$model = new $model;
9797

9898
// if fillable was defined, use that as the attributes
99-
if (! count($model->getFillable())) {
99+
if (count($model->getFillable())) {
100100
$attributes = $model->getFillable();
101101
} else {
102102
// otherwise, if guarded is used, just pick up the columns straight from the bd table
@@ -116,8 +116,8 @@ protected function getAttributes($model)
116116
*/
117117
protected function replaceSetFromDb(&$stub, $name)
118118
{
119-
$class = str_replace($this->getNamespace($name).'\\', '', $name);
120-
$model = 'App\Models\\'.$class;
119+
$class = Str::afterLast($name, '\\');
120+
$model = "App\\Models\\$class";
121121

122122
if (! class_exists($model)) {
123123
return $this;
@@ -126,24 +126,20 @@ protected function replaceSetFromDb(&$stub, $name)
126126
$attributes = $this->getAttributes($model);
127127

128128
// create an array with the needed code for defining fields
129-
$fields = Arr::where($attributes, function ($value, $key) {
130-
return ! in_array($value, ['id', 'created_at', 'updated_at', 'deleted_at']);
131-
});
132-
if (count($fields)) {
133-
foreach ($fields as $key => $field) {
134-
$fields[$key] = "CRUD::field('".$field."');";
135-
}
136-
}
129+
$fields = Arr::except($attributes, ['id', 'created_at', 'updated_at', 'deleted_at']);
130+
$fields = collect($fields)
131+
->map(function ($field) {
132+
return "CRUD::column('$field');";
133+
})
134+
->toArray();
137135

138136
// create an array with the needed code for defining columns
139-
$columns = Arr::where($attributes, function ($value, $key) {
140-
return ! in_array($value, ['id']);
141-
});
142-
if (count($columns)) {
143-
foreach ($columns as $key => $column) {
144-
$columns[$key] = "CRUD::column('".$column."');";
145-
}
146-
}
137+
$columns = Arr::except($attributes, ['id']);
138+
$columns = collect($columns)
139+
->map(function ($column) {
140+
return "CRUD::column('$column');";
141+
})
142+
->toArray();
147143

148144
// replace setFromDb with actual fields and columns
149145
$stub = str_replace('CRUD::setFromDb(); // fields', implode(PHP_EOL.' ', $fields), $stub);

0 commit comments

Comments
 (0)