Skip to content

Commit

Permalink
add withCount to transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Antonets committed Nov 19, 2024
1 parent da82b5b commit 6107ff9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Attributes/ResponseFromTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(

/* Format: [adapter, numberPerPage]. Example: [SomePaginator::class, 10] */
public array $paginate = [],
public array $withCount = [],
) {
}
}
5 changes: 3 additions & 2 deletions src/Extracting/InstantiatesExampleModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ protected function getExampleModelFromFactoryMake(string $type, array $factorySt
/**
* @param class-string $type
* @param string[] $relations
* @param string[] $withCount
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
protected function getExampleModelFromDatabaseFirst(string $type, array $relations = [])
protected function getExampleModelFromDatabaseFirst(string $type, array $relations = [], array $withCount = [])
{
return $type::with($relations)->first();
return $type::with($relations)->withCount($withCount)->first();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function getTransformerResponse(ResponseFromTransformer $attributeInst
{
$modelInstantiator = fn() => $this->instantiateExampleModel(
$attributeInstance->model, $attributeInstance->factoryStates, $attributeInstance->with,
(new ReflectionClass($attributeInstance->name))->getMethod('transform')
(new ReflectionClass($attributeInstance->name))->getMethod('transform'), $attributeInstance->withCount,
);

$pagination = $attributeInstance->paginate ? [
Expand Down

0 comments on commit 6107ff9

Please sign in to comment.