Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function newRepository()
{
$repository = $this->repository();

return new $repository($repository::newModel());
return resolve($repository, [
'model' => $repository::newModel(),
])->withResource($repository::newModel());
}

/**
Expand Down Expand Up @@ -115,7 +117,9 @@ public function newRepositoryWith($model)
{
$repository = $this->repository();

return new $repository($model);
return resolve($repository, [
'model' => $model,
])->withResource($model);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Repositories/Crudable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
6 changes: 5 additions & 1 deletion src/Repositories/RepositoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down