@@ -80,6 +80,13 @@ abstract class EloquentAdapter implements AdapterInterface
80
80
*/
81
81
protected $ defaultPagination = null ;
82
82
83
+ /**
84
+ * Default eager loading when querying many resources.
85
+ *
86
+ * @var string[]|null
87
+ */
88
+ protected $ with = null ;
89
+
83
90
/**
84
91
* Apply the supplied filters to the builder instance.
85
92
*<
8000
/span>
@@ -115,12 +122,13 @@ public function __construct(Model $model, PagingStrategyInterface $paging = null
115
122
public function query (EncodingParametersInterface $ parameters )
116
123
{
117
124
$ filters = $ this ->extractFilters ($ parameters );
125
+ $ this ->with ($ query = $ this ->newQuery (), $ this ->extractIncludePaths ($ parameters ));
118
126
119
127
if ($ this ->isFindMany ($ filters )) {
120
- return $ this ->findMany ($ filters );
128
+ return $ this ->findMany ($ query , $ filters );
121
129
}
122
130
123
- $ this ->filter ($ query = $ this -> newQuery () , $ filters );
131
+ $ this ->filter ($ query , $ filters );
124
132
$ this ->sort ($ query , (array ) $ parameters ->getSortParameters ());
125
133
126
134
if ($ this ->isSearchOne ($ filters )) {
@@ -166,13 +174,28 @@ protected function newQuery()
166
174
}
167
175
168
176
/**
177
+ * Add eager loading to the query.
178
+ *
179
+ * @param Builder $query
180
+ * @param Collection $includePaths
181
+ * the paths for resources that will be included.
182
+ * @return array
183
+ */
184
+ protected function with (Builder $ query , Collection $ includePaths )
185
+ {
186
+ if ($ this ->with ) {
187
+ $ query ->with ($ this ->with );
188
+ }
189
+ }
190
+
191
+ /**
192
+ * @param Builder $query
169
193
* @param Collection $filters
170
194
* @return mixed
171
195
*/
172
- protected function findMany (Collection $ filters )
196
+ protected function findMany (Builder $ query , Collection $ filters )
173
197
{
174
- return $ this
175
- ->newQuery ()
198
+ return $ query
176
199
->whereIn ($ this ->getQualifiedKeyName (), $ this ->extractIds ($ filters ))
177
200
->get ();
178
201
}
@@ -229,6 +252,15 @@ protected function getQualifiedKeyName()
229
252
return sprintf ('%s.%s ' , $ this ->model ->getTable (), $ this ->getKeyName ());
230
253
}
231
254
255
+ /**
256
+ * @param EncodingParametersInterface $parameters
257
+ * @return Collection
258
+ */
259
+ protected function extractIncludePaths (EncodingParametersInterface $ parameters )
260
+ {
261
+ return new Collection ((array ) $ parameters ->getIncludePaths ());
262
+ }
263
+
232
264
/**
233
265
* @param EncodingParametersInterface $parameters
234
266
* @return Collection
0 commit comments