Laravel QueryBuilder…GroupBy和Count

Laravel QueryBuilder…GroupBy和Count,laravel,eloquent,Laravel,Eloquent,我爬网我的网站并保存爬网状态代码(200301等)和爬网深度(0,1,2,3等) 我的数据库/模型中的每个输入项都包含:url、状态代码、爬网深度 我尝试为每个爬网深度级别获取所有不同的状态代码,为此,我尝试从Eloquent或DB::query构建一个数组(或集合) An array where index is the crawl_depth $array[crawl_depth] = [ '200' => '114' //num of url with sta

我爬网我的网站并保存爬网状态代码(200301等)和爬网深度(0,1,2,3等) 我的数据库/模型中的每个输入项都包含:url、状态代码、爬网深度

我尝试为每个爬网深度级别获取所有不同的状态代码,为此,我尝试从Eloquent或DB::query构建一个数组(或集合)

An array where index is the crawl_depth
$array[crawl_depth] = [
            '200' => '114' //num of url with status code '200' in this crawl_depth
            '301' => '115' // num of 301
            etc...
         ]
我需要构建查询,但我没有找到好的查询,我有一个解决方案,但与我试图得到的相去甚远

$crawlDepth=DB::table('crawl\u url')
->其中('project_id','=',$this->project->id)
->选择('crawl_depth',DB::raw('count(*)as UrlOnthisDepth'))
->groupBy(“爬网深度”)
->get();
通过此查询,我可以获得每个级别的count url:

 Illuminate\Support\Collection {#1445 ▼
  #items: array:6 [▼
    0 => {#1486 ▼
      +"crawl_depth": 0
      +"UrlOnthisDepth": 1
    }
    1 => {#1447 ▼
      +"crawl_depth": 1
      +"UrlOnthisDepth": 52
    }
    2 => {#1488 ▼
      +"crawl_depth": 2
      +"UrlOnthisDepth": 215
    }
    3 => {#1490 ▼
      +"crawl_depth": 3
      +"UrlOnthisDepth": 563
    }
    4 => {#1491 ▼
      +"crawl_depth": 4
      +"UrlOnthisDepth": 341
    }
    5 => {#1492 ▼
      +"crawl_depth": 5
      +"UrlOnthisDepth": 6
    }
  ]
}
对于另一个查询:

$crawlDepth = DB::table('crawl_urls')
            ->where('project_id', '=', $this->project->id)
            ->select('crawl_depth', 'status_code')
            ->groupBy('crawl_depth', 'status_code')
            ->get();
我得到了这个结果,到目前为止还没有,但是我错过了每个状态码的url数量

Illuminate\Support\Collection {#1368 ▼
  #items: array:11 [▼
    0 => {#1488 ▼
      +"crawl_depth": 0
      +"status_code": 200
    }
    1 => {#1486 ▼
      +"crawl_depth": 1
      +"status_code": 200
    }
    2 => {#1487 ▼
      +"crawl_depth": 1
      +"status_code": 302
    }
    3 => {#1489 ▼
      +"crawl_depth": 1
      +"status_code": 500
    }
    4 => {#1490 ▼
      +"crawl_depth": 2
      +"status_code": 200
    }
    5 => {#1491 ▼
      +"crawl_depth": 2
      +"status_code": 302
    }
    6 => {#1492 ▼
      +"crawl_depth": 3
      +"status_code": 200
    }
    7 => {#1493 ▼
      +"crawl_depth": 3
      +"status_code": 302
    }
    8 => {#1494 ▼
      +"crawl_depth": 4
      +"status_code": 200
    }
    9 => {#1495 ▼
      +"crawl_depth": 4
      +"status_code": 302
    }
    10 => {#1496 ▼
      +"crawl_depth": 5
      +"status_code": 200
    }
  ]
}

从雄辩的查询中获得特定的输出将是困难的(如果不是不可能的话)。您只需提取数据并在之后重新构造它,可能会更好

使用此查询:

$crawlDepth = DB::table('crawl_urls')
            ->where('project_id', '=', $this->project->id)
            ->select('crawl_depth', 'status_code', DB::raw('count(*) as UrlOnthisDepth'))
            ->groupBy('crawl_depth', 'status_code')
            ->get();
您将得到一个类似于
[[crawl\u depth0,status\u code0,count0],[crawl\u depth1,status\u code1,count1],…]的集合。然后,您可以通过迭代结果(未经测试的代码,但您得到了这个想法)来重新构造它:


从雄辩的查询中获得特定的输出将是困难的(如果不是不可能的话)。您只需提取数据并在之后重新构造它,可能会更好

使用此查询:

$crawlDepth = DB::table('crawl_urls')
            ->where('project_id', '=', $this->project->id)
            ->select('crawl_depth', 'status_code', DB::raw('count(*) as UrlOnthisDepth'))
            ->groupBy('crawl_depth', 'status_code')
            ->get();
您将得到一个类似于
[[crawl\u depth0,status\u code0,count0],[crawl\u depth1,status\u code1,count1],…]的集合。然后,您可以通过迭代结果(未经测试的代码,但您得到了这个想法)来重新构造它:

对于以下数据集

1,a,300,2
2,a,200,1
3,a,300,4
4,b,200,5
5,c,200,1
6,a,300,1
它将提供以下json

{
  "1": [
    {
      "status_code": 200,
      "UrlOnthisDepth": 2
    },
    {
      "status_code": 300,
      "UrlOnthisDepth": 1
    }
  ],
  "2": [
    {
      "status_code": 300,
      "UrlOnthisDepth": 1
    }
  ],
  "4": [
    {
      "status_code": 300,
      "UrlOnthisDepth": 1
    }
  ],
  "5": [
    {
      "status_code": 200,
      "UrlOnthisDepth": 1
    }
  ]
}
对于以下数据集

1,a,300,2
2,a,200,1
3,a,300,4
4,b,200,5
5,c,200,1
6,a,300,1
它将提供以下json

{
  "1": [
    {
      "status_code": 200,
      "UrlOnthisDepth": 2
    },
    {
      "status_code": 300,
      "UrlOnthisDepth": 1
    }
  ],
  "2": [
    {
      "status_code": 300,
      "UrlOnthisDepth": 1
    }
  ],
  "4": [
    {
      "status_code": 300,
      "UrlOnthisDepth": 1
    }
  ],
  "5": [
    {
      "status_code": 200,
      "UrlOnthisDepth": 1
    }
  ]
}