Eloquent 带有子项目数的请求

Eloquent request with subitems count

我有 2 个表:

和数据透视表(这里不是很重要)。

我有一个方法 sql 请求...

我尝试使用 map 函数来添加一个列,其中包含每个价值链的细分数量......

public function vcListAndSegmentCount() {
    $valuechainLists = Valuechain::select('valuechains.id', 'lang_valuechain.vcname', 'lang_valuechain.vcshortname')
        ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
        ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
        ->where('langs.isMainlanguage', '=', '1')
        ->whereNull('valuechains.deleted_at')
        ->get();

    $valuechainCount = Valuechain::whereNull('valuechains.deleted_at')->count();

    for ($i=0; $i < $valuechainCount; $i++) {
        $segmentCount[$i] = Segment::whereNull('segments.deleted_at')
            ->where('valuechain_id', '=', $valuechainLists[$i]->id)->count();
    }

    $valuechainLists = $valuechainLists->map(function ($record) use ($segmentCount) {
        $vclists = array_first($segmentCount, function ($value, $key) use ($record) {
            return $value['id'] === $record['valuechain_id'];
        });
        $record['count'] = $vclists;
        return $record;

    });
    dd($valuechainLists);
}

地图方法在我的输出集合中添加了一列。不幸的是,新系列没有为每个价值链提供正确数量的细分……它只增加了一个价值……

这是我得到的:

Collection {#380 ▼
  #items: array:4 [▼
    0 => Valuechain {#450 ▼
      ...
      #attributes: array:4 [▼
        "id" => 1
        "vcname" => "Génétique"
        "vcshortname" => "Génétique"
        "count" => 6
      ]
      #original: array:3 [▶]
      ...
    }
    1 => Valuechain {#451 ▼
      ...
      #attributes: array:4 [▼
        "id" => 2
        "vcname" => "Biotruc"
        "vcshortname" => "Biotruc"
        "count" => 6
      ]
      ...
    }
    2 => Valuechain {#452 ▼
      ...
      #attributes: array:4 [▼
        "id" => 3
        "vcname" => "VC3"
        "vcshortname" => "VC3"
        "count" => 6
      ]
      ...
    }
    3 => Valuechain {#453 ▼
      ...
      #attributes: array:4 [▼
        "id" => 4
        "vcname" => "VC4"
        "vcshortname" => "VC4"
        "count" => 6
      ]
      #original: array:3 [▶]
      ...
    }
  ]
}

我得到 6、6、6 和 6 而计数应该是 6、5、4、4...

将计数值添加为 属性 而不是数组元素。

 $valuechainLists = $valuechainLists->map(function ($record) use ($segmentCount) {
     $vclists = array_first($segmentCount, function ($value, $key) use ($record) {
         return $value['id'] === $record['valuechain_id'];
     });
     $record->count = $vclists;
     return $record;
 });

我改变了

$record['count'] = $vclists;

$record->count = $vclists;

添加参考:&$segmentCount

$valuechainLists = $valuechainLists->map(function ($record) use (&$segmentCount) {....

我也不确定你的 array_first 函数在做什么。

如果您正在使用 Laravel >= 5.2 并且您已经在模型上定义了关系,则可以使用 withCount() 方法。

它会是这样的:

 Valuechain::select('valuechains.id', 'lang_valuechain.vcname', 'lang_valuechain.vcshortname')
    ->withCount(['segments' => function ($query) {
        $query->whereNull('deleted_at);
    }])
    ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
    ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
    ->where('langs.isMainlanguage', '=', '1')
    ->whereNull('valuechains.deleted_at')
    ->get()

或者如果您的 Segment 模型使用 SoftDeletes 特征,那么它会更简单一些:

Valuechain::select('valuechains.id', 'lang_valuechain.vcname', 'lang_valuechain.vcshortname')
    ->withCount('segments')
    ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
    ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
    ->where('langs.isMainlanguage', '=', '1')
    ->whereNull('valuechains.deleted_at')
    ->get()