使用 $this->response->collection 返回空数组的时候会报错
经常使用$this->response->collection
来返回筛选到的一组列表数组,但如果这个数组是空的,就会发生报错
如以下代码
$category = Category::where('organization_id', $organization_id)->where('up_id', $specialty_category_info->id)->get();
return $this->response->collection($category, new CategoryTransformer());
通过条件筛选出一组分类列表,分类列表可能是空,经测试,为空数组时报错,不为空时全部正常,报错摘要如下:
"message": "Type error: Argument 1 passed to Dingo\\Api\\Http\\Response\\Factory::collection() must be an instance of Illuminate\\Support\\Collection, array given, called in /www/wwwroot/app/Http/Controllers/Api/V1/AboutController.php on line 159",
"status_code": 500,
希望能告知这是正常现象,需要我手动去判断,还是有某个地方没处理好导致的,谢谢
推荐文章: