I'm in struggle because Dingo seems to transform only Eloquent collections and objects. I could solve returning the collection with the method Collection::make().
But how is it possible to return a single user object? (see method getUser())
<?php
namespace App\Http\Controllers;
use App\Entities\User;
use App\Transformer\UserTransformer;
use Dingo\Api\Http\Response;
use Dingo\Api\Routing\Helpers;
use Doctrine\ORM\EntityManagerInterface;
use App\Http\Requests;
use Illuminate\Routing\Controller;
use Illuminate\Support\Collection;
class UsersController extends Controller
{
use Helpers;
protected $em;
/**
* UsersController constructor.
* @param $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getUser($id)
{
$user = $this->em->getRepository(User::class)->findOneBy(array('id' => $id));
return $user;
}
public function getUsers()
{
$user = $this->em->getRepository(User::class)->findAll();
return $this->response->collection(Collection::make($user), new UserTransformer);
}
}
Luckily I have found a working solution now. I want to share this and hope to be able to help other people:
return $this->response->item($user, new UserTransformer());