我用Doctrine(laravel-doctrine / orm)设置了Lumen
当我尝试在HTTP路由上获取结果(即$ this-&gt; em-&gt; getRepository(Student :: class) - &gt; findAll();)时,我得到一个空的大括号数组。< / p>
如何正确运行序列化?
答案 0 :(得分:1)
首先,您需要使用Illuminate\Support\Collection
类包装结果,例如:
use Illuminate\Support\Collection;
return Collection::make(
$this->em->getRepository(Student::class)->findAll()
);
之后,修改您的Student
课程,将其分配给Illuminate\Contracts\Support\Arrayable
合同。
use Illuminate\Contracts\Support\Arrayable;
class Student implements Arrayable
{
// Your code here
}
接下来,您应该为toArray
课程实施Student
方法:
use Illuminate\Contracts\Support\Arrayable;
class Student implements Arrayable
{
public function toArray()
{
return [
'id' => $this->getId(),
// ... and so on ...
];
}
}