I've got this working, if you still need it?
Basically, I'm including my transformer at the response stage, so my Controller looks something like this:
public function index()
{
return Response::api()->withCollection($this->repository->all(), new PointsTransformer);
}
So my repository is just returning a Doctrine array collection and then my transformer is exactly like the Dingo documentation.
Good luck!
I'm getting this error:
"Argument 1 passed to Dingo\Api\Http\Response\Factory::collection() must be an instance of Illuminate\Support\Collection, array given"
public function index(UserService $userService)
{
return $this->response->withCollection($userService->getAll(), new UserTransformer);
}
Basically I'm doing a repository->findAll() query, which returns an array of Doctrine entities.
My transformer looks like this:
class UserTransformer extends TransformerAbstract
{
/**
* @param UserModel $user
*
* @return array
*/
public function transform(UserModel $user)
{
return [
'id' => $user->getId(),
'firstName' => $user->getName1(),
'lastName' => $user->getName3(),
'email' => $user->getEmail()
];
}
}
Any idea ?
Finally I found the solution:
The array provided by Doctrine should be converted into a Collection before it is passed to the Dingo Result:
use Illuminate\Support\Collection;
....
$this->response->withCollection(Collection::make($userService->getAll()), new UserTransformer);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community