群主,Transformer 这个方法非常好用,但我遇到一个问题
dingo api 下面有两个方法:
$this->response->item 将一个项目绑定到transformer并启动响应
$this->response->collection 将集合绑定到transformer并启动响应
从数据库从取出数据直接返回,通过以上两个方法均正常,但是,如果从数据库取出数据经过处理的,例如:
// 从购物车中取出数据
$cartItems = self::$cartStore->apiGetCartAll($where);
// 购物车数据中添加相应商品信息
foreach ($cartItems as $cart_item)
{
$cart_item->goods = self::$goodsStore
->apiGetGoods($cart_item->guid);
array_push($cartItems_arr, $cart_item);
}
return $cartitems_arr
这里返回的数据是经过处理的,然后用以上方法均有报错
Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Dingo\Api\Http\Response\Factory::collection() must be an instance of Illuminate\Support\Collection, array given, called in /vagrant/tb100/admin/app/Api/Controllers/v1/CartController.php on line 41 in /vagrant/tb100/admin/vendor/dingo/api/src/Http/Response/Factory.php:96
ErrorException: get_class() expects parameter 1 to be object, array given in /vagrant/tb100/admin/vendor/dingo/api/src/Http/Response/Factory.php:126