laravel雄辩地从对象中提取数组

时间:2020-08-04 19:02:17

标签: laravel eloquent

在我的控制器中,我返回一个查询,并希望将结果转换为可以使用的数组。在我的单元测试中,我想确保返回数组是三的计数。

我不断得到这个

+baseResponse: Illuminate\Http\JsonResponse^ {#3966
    #data: "[{"id":0,"title":"Alphabet","content":"A, B, C","views":"8","timestamp":"1596567476","created_at":"2020-08-04 18:57:56","updated_at":"2020-08-04 18:57:56"},{"id":0,"title":"Alphabet","content":"A, B, C","views":"5","timestamp":"1596567476","created_at":"2020-08-04 18:57:56","updated_at":"2020-08-04 18:57:56"},{"id":0,"title":"Alphabet","content":"A, B, C","views":"0","timestamp":"1596567476","created_at":"2020-08-04 18:57:56","updated_at":"2020-08-04 18:57:56"}]"
    #callback: null
    #encodingOptions: 0
    +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag^ {#3996
      #computedCacheControl: array:2 [
        "no-cache" => true
        "private" => true
      ]
      #cookies: []
      #headerNames: array:3 [
        "cache-control" => "Cache-Control"
        "date" => "Date"
        "content-type" => "Content-Type"
      ]
      #headers: array:3 [
        "cache-control" => array:1 [
          0 => "no-cache, private"
        ]
        "date" => array:1 [
          0 => "Tue, 04 Aug 2020 18:57:56 GMT"
        ]
        "content-type" => array:1 [
          0 => "application/json"
        ]
      ]
      #cacheControl: []
    }
    #content: "[{"id":0,"title":"Alphabet","content":"A, B, C","views":"8","timestamp":"1596567476","created_at":"2020-08-04 18:57:56","updated_at":"2020-08-04 18:57:56"},{"id":0,"title":"Alphabet","content":"A, B, C","views":"5","timestamp":"1596567476","created_at":"2020-08-04 18:57:56","updated_at":"2020-08-04 18:57:56"},{"id":0,"title":"Alphabet","content":"A, B, C","views":"0","timestamp":"1596567476","created_at":"2020-08-04 18:57:56","updated_at":"2020-08-04 18:57:56"}]"
    #version: "1.1"
    #statusCode: 200
    #statusText: "OK"
    #charset: null
    +original: array:3 [
      0 => array:7 [
        "id" => 0
        "title" => "Alphabet"
        "content" => "A, B, C"
        "views" => "8"
        "timestamp" => "1596567476"
        "created_at" => "2020-08-04 18:57:56"
        "updated_at" => "2020-08-04 18:57:56"
      ]
      1 => array:7 [
        "id" => 0
        "title" => "Alphabet"
        "content" => "A, B, C"
        "views" => "5"
        "timestamp" => "1596567476"
        "created_at" => "2020-08-04 18:57:56"
        "updated_at" => "2020-08-04 18:57:56"
      ]
      2 => array:7 [
        "id" => 0
        "title" => "Alphabet"
        "content" => "A, B, C"
        "views" => "0"
        "timestamp" => "1596567476"
        "created_at" => "2020-08-04 18:57:56"
        "updated_at" => "2020-08-04 18:57:56"
      ]
    ]
    +exception: null
  }
  #streamedContent: null

我只想要这个

  0 => array:7 [
        "id" => 0
        "title" => "Alphabet"
        "content" => "A, B, C"
        "views" => "8"
        "timestamp" => "1596567476"
        "created_at" => "2020-08-04 18:57:56"
        "updated_at" => "2020-08-04 18:57:56"
      ]
      1 => array:7 [
        "id" => 0
        "title" => "Alphabet"
        "content" => "A, B, C"
        "views" => "5"
        "timestamp" => "1596567476"
        "created_at" => "2020-08-04 18:57:56"
        "updated_at" => "2020-08-04 18:57:56"
      ]
      2 => array:7 [
        "id" => 0
        "title" => "Alphabet"
        "content" => "A, B, C"
        "views" => "0"
        "timestamp" => "1596567476"
        "created_at" => "2020-08-04 18:57:56"
        "updated_at" => "2020-08-04 18:57:56"
      ]

这是我的控制器的代码。

 public function retrieveStore(Request $request)
    {
        $params = $request->query("query");
        $params = $this->refineParams("/LESS_THAN\s*\(.+\)/m", "<", $params);
        $params = $this->refineParams("/GREATER_THAN\s*\(.+\)/m", ">", $params);
        $params = $this->refineParams("/EQUAL\s*\(.+\)/m", "=", $params);
     
        $json = Post::whereRaw($params)->get();
        return response()->json($json->toArray(), 200);
    }

    private function refineParams($regex, $symbol, $params)
    {
        $fields = ["id" => 0, "title" => 0, "content" => 0, "views" => 0, "timestamp" => 0];
        preg_match_all($regex, $params,
        $out, PREG_PATTERN_ORDER);
        foreach($out as $key){
            $str = implode($key);
            $begining = strpos($str, '(') + 1;
            $end = strpos($str, ',') -  $begining;
            $firstValue = substr($str,  $begining , $end);
            $firstValue  = str_replace(' ', '', $firstValue);
            $begining = strpos($str, ',') + 1;
            $end = strpos($str, ')') -  $begining;
            $endValue = substr($str,  $begining , $end);
            $endValue = str_replace(' ', '', $endValue);
            if(array_search($firstValue, $fields) > -1 && ($endValue === "0" || (int)$endValue > 0)){
                $params = str_replace($str, $firstValue . $symbol . $endValue, $params);
            }
        }
        return $params;
    }

我缺少将结果转换为可以使用的数组的什么地方?

1 个答案:

答案 0 :(得分:2)

由于直接调用Controller方法并对其进行单元测试,因此您将获得返回的内容,在本例中为JsonResponse对象。您仍然可以获取该对象的内容。

如果您希望在发送响应后内容是什么样的,可以通过$response->content()$response->getContent()获得。如果内容应为JSON,它将对其进行转换并将响应的$content设置为该JSON。

如果您想要实际的原始数据作为此响应的内容,则可以专门获得该信息:

$response->getOriginalContent();

那应该给您原始阵列。