|
5 | 5 | use Binaryk\LaravelRestify\Contracts\RestifySearchable; |
6 | 6 | use Binaryk\LaravelRestify\Exceptions\Guard\EntityNotFoundException; |
7 | 7 | use Binaryk\LaravelRestify\Exceptions\Guard\GatePolicy; |
| 8 | +use Binaryk\LaravelRestify\Exceptions\InstanceOfException; |
8 | 9 | use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
| 10 | +use Binaryk\LaravelRestify\Repositories\Repository; |
9 | 11 | use Binaryk\LaravelRestify\Services\Search\SearchService; |
10 | 12 | use Binaryk\LaravelRestify\Traits\PerformsQueries; |
11 | | -use Illuminate\Config\Repository; |
12 | 13 | use Illuminate\Config\Repository as Config; |
13 | 14 | use Illuminate\Container\Container; |
14 | 15 | use Illuminate\Contracts\Auth\Access\Gate; |
@@ -84,8 +85,8 @@ public function config() |
84 | 85 | { |
85 | 86 | $container = Container::getInstance(); |
86 | 87 |
|
87 | | - if (($this->config instanceof Repository) === false) { |
88 | | - $this->config = $container->make(Repository::class); |
| 88 | + if (($this->config instanceof Config) === false) { |
| 89 | + $this->config = $container->make(Config::class); |
89 | 90 | } |
90 | 91 |
|
91 | 92 | return $this->config; |
@@ -130,18 +131,27 @@ protected function response($data = null, $status = 200, array $headers = []) |
130 | 131 | * @param array $filters |
131 | 132 | * @return array |
132 | 133 | * @throws BindingResolutionException |
| 134 | + * @throws InstanceOfException |
133 | 135 | */ |
134 | 136 | public function search($modelClass, $filters = []) |
135 | 137 | { |
136 | 138 | $results = SearchService::instance() |
137 | 139 | ->setPredefinedFilters($filters) |
138 | | - ->search($this->request(), new $modelClass); |
| 140 | + ->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass); |
| 141 | + |
139 | 142 | $results->tap(function ($query) { |
140 | 143 | static::indexQuery($this->request(), $query); |
141 | 144 | }); |
142 | 145 |
|
| 146 | + /** |
| 147 | + * @var \Illuminate\Pagination\Paginator |
| 148 | + */ |
143 | 149 | $paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE)); |
144 | | - $items = $paginator->getCollection()->map->serializeForIndex($this->request()); |
| 150 | + if ($modelClass instanceof Repository) { |
| 151 | + $items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->serializeForIndex($this->request()); |
| 152 | + } else { |
| 153 | + $items = $paginator->getCollection()->map->serializeForIndex($this->request()); |
| 154 | + } |
145 | 155 |
|
146 | 156 | return array_merge($paginator->toArray(), [ |
147 | 157 | 'data' => $items, |
|
0 commit comments