Support the ongoing development of Laravel.io →
Database Packages
Last updated 2 years ago.
0

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!

Last updated 2 years ago.
0
Solution

Thanks a lot.

Last updated 2 years ago.
0

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 ?

Last updated 8 years ago.
0

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);
0

Sign in to participate in this thread!

Eventy

Your banner here too?

psren psren Joined 9 Dec 2013

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.