@@ -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