diff --git a/src/Http/Requests/InteractWithRepositories.php b/src/Http/Requests/InteractWithRepositories.php index adff405c0..bc8f20c2e 100644 --- a/src/Http/Requests/InteractWithRepositories.php +++ b/src/Http/Requests/InteractWithRepositories.php @@ -84,7 +84,9 @@ public function newRepository() { $repository = $this->repository(); - return new $repository($repository::newModel()); + return resolve($repository, [ + 'model' => $repository::newModel(), + ])->withResource($repository::newModel()); } /** @@ -115,7 +117,9 @@ public function newRepositoryWith($model) { $repository = $this->repository(); - return new $repository($model); + return resolve($repository, [ + 'model' => $model, + ])->withResource($model); } /** diff --git a/src/Repositories/Crudable.php b/src/Repositories/Crudable.php index fcfe98282..14f505f61 100644 --- a/src/Repositories/Crudable.php +++ b/src/Repositories/Crudable.php @@ -22,7 +22,9 @@ trait Crudable */ public function index(RestifyRequest $request, Paginator $paginated) { - return (new static($paginated))->response(); + return resolve(static::class, [ + 'model' => $paginated, + ])->withResource($paginated)->response(); } /** diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index b239cc0db..7c0cd9533 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -41,10 +41,9 @@ abstract class Repository extends RepositoryCollection implements RestifySearcha * * @param \Illuminate\Database\Eloquent\Model $model */ - public function __construct($model) + public function __construct($model = null) { parent::__construct($model); - $this->resource = $model; } /** @@ -126,4 +125,15 @@ public function collectFields(RestifyRequest $request) { return collect($this->fields($request)); } + + /** + * @param $resource + * @return Repository + */ + public function withResource($resource) + { + $this->resource = $resource; + + return $this; + } } diff --git a/src/Repositories/RepositoryCollection.php b/src/Repositories/RepositoryCollection.php index c0505a4ff..1debf3c42 100644 --- a/src/Repositories/RepositoryCollection.php +++ b/src/Repositories/RepositoryCollection.php @@ -38,7 +38,11 @@ public function toArrayForCollection($request) $iterator->next(); } - $response = $data->mapInto($currentRepository)->toArray($request); + $response = $data->map(function ($value) use ($currentRepository) { + return resolve($currentRepository, [ + 'model' => $value, + ])->withResource($value); + })->toArray($request); return [ 'meta' => $this->when($this->isRenderingPaginated(), $this->meta($paginated)),