2016-06-20 50 views
0

我設置流明與學說(laravel-主義/ ORM)流明+主義的findAll返回空對象數組

當我嘗試獲取結果(即$這個 - > EM-> getRepository(學生::類) - > findAll();)在一個HTTP路由,我得到一個空的大括號數組。

如何使序列化正確運行?

回答

1

首先,你需要Illuminate\Support\Collection類,像包裹你的結果:

use Illuminate\Support\Collection; 

return Collection::make(
    $this->em->getRepository(Student::class)->findAll() 
); 

之後,通過將其分配到Illuminate\Contracts\Support\Arrayable合同修改Student類。

use Illuminate\Contracts\Support\Arrayable; 

class Student implements Arrayable 
{ 
    // Your code here 
} 

接下來,你應該實現toArray方法您Student類: