当 returned 的记录超过 2 条时,为什么 Laravel 不再 return 包含在数组中的响应?

Why does Laravel no longer return a response wrapped in an array when more than 2 records are returned?

从端点返回响应时,Laravel 遇到一个奇怪的问题。

当通过我的端点返回的记录少于两条时,我收到此响应:

然而,当该端点检索到两条以上的记录时,我得到以下响应:

如您所见,json 响应现在包装在对象而不是数组中。这是一个问题,因为我在客户端使用 var.length 来进行此响应。当我使用 .length 返回超过两条记录时失败,因为它作为对象而不是数组返回。

知道为什么会这样吗?这是我的控制器函数,returns 数据:

/**
 * Return active failures
 */
public function getActiveFailures()
{
  $test_failures_active = TestFailure::where('active', '=', true)
    ->with('type', 'scope', 'error_type')
    ->orderBy('created_at', 'DESC')
    ->get();
  $test_failures_unique = $test_failures_active->unique('error_message');
  return $test_failures_unique;
}

当您使用 unique 时,您集合中的一些值已被删除,但键保持不变。由于键的 0 索引顺序被破坏,json_encodeing 将您的集合视为 object 而不是 array。您需要做的是使用 values() 方法重新索引项目:

return $test_failures_unique->values();