@@ -26,9 +26,7 @@ public function __construct(string $operation, array $clauses = [])
26
26
$ this ->operation = $ operation ;
27
27
$ this ->clauses = $ clauses ;
28
28
29
- if ($ operation === 'all ' && !empty ($ clauses )) {
30
- $ this ->model = Str::studly (Str::singular ($ clauses [0 ]));
31
- }
29
+ $ this ->determineModel ($ this ->model );
32
30
}
33
31
34
32
public function operation (): string
@@ -51,11 +49,11 @@ public function output(string $controller): string
51
49
$ model = $ this ->determineModel ($ controller );
52
50
53
51
if ($ this ->operation () === 'all ' ) {
54
- if ( is_null ( $ this -> model ())) {
55
- return ' $ ' . Str:: camel (Str:: plural ( $ model )) . ' = ' . $ model . ' ::all(); ' ;
56
- } else {
57
- return ' $ ' . Str:: camel ($ this ->clauses ()[ 0 ]) . ' = ' . $ this -> model () . ' ::all(); ' ;
58
- }
52
+ return ' $ ' . Str:: camel (Str:: plural ( $ model)) . ' = ' . $ model . ' ::all(); ' ;
53
+ }
54
+
55
+ if ($ this ->operation () === ' paginate ' ) {
56
+ return ' $ ' . Str:: camel (Str:: plural ( $ model )) . ' = ' . $ model . ' ::paginate(); ' ;
59
57
}
60
58
61
59
$ methods = [];
@@ -117,15 +115,23 @@ private function pluckName(string $field)
117
115
return Str::lower (Str::plural (str_replace ('. ' , '_ ' , $ field )));
118
116
}
119
117
120
- return Str::lower (' Post ' . '_ ' . Str::plural ($ field ));
118
+ return Str::lower ($ this -> model . '_ ' . Str::plural ($ field ));
121
119
}
122
120
123
- private function determineModel (string $ prefix )
121
+ private function determineModel (? string $ controller )
124
122
{
125
- if (empty ($ this ->model ())) {
126
- return Str::studly (Str::singular ($ prefix ));
123
+ if (! is_null ($ controller ) && ! empty ($ controller )) {
124
+ $ this ->model = Str::studly (Str::singular ($ controller ));
125
+ }
126
+
127
+ if (
128
+ is_null ($ this ->model ()) &&
129
+ ! empty ($ this ->clauses ()) &&
130
+ ! in_array ($ this ->operation (), ['count ' ,'exists ' ])
131
+ ) {
132
+ $ this ->model = Str::studly (Str::singular (Str::before (Str::after ($ this ->clauses ()[0 ], ': ' ), '. ' )));
127
133
}
128
134
129
- return Str:: studly ( $ this ->model () );
135
+ return $ this ->model ();
130
136
}
131
137
}
0 commit comments