drip5880 2018-05-15 20:35
浏览 11
已采纳

具有子项目的雄辩请求计数

I have 2 tables :

  • Valuechains : id, created_at, updated_at, deleted_at
  • Segments : id, valuechain_id (Foreign key), created_at, updated_at, deleted_at

And pivot tables (not really important here).

I have a method with sql requests ...

  • $valuechains list gives me a list of all the value chains which are not (soft) deleted
  • $valuechainCount counts the number of valuechains which are published
  • $segmentCount counts the number of segments for each value chains

I try to use the map function in order to add a column which contains the number of segments for each value chains ...

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);
}

The map methods adds a column my output collection. Unfortunatelly, the new collection is not giving me the right numbers of segments for each value chain... it only adds one value ...

Here is what I obtain :

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 [▶]
      ...
    }
  ]
}

I obtain 6, 6, 6 and 6 whereas the count should be 6, 5, 4, 4...

  • 写回答

3条回答 默认 最新

  • dongma1666 2018-05-23 08:19
    关注

    If you are using Laravel >= 5.2 and if you have defined the relationships on the models, you can use the withCount() method.

    It would go something like this:

     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()
    

    or if your Segment model uses the SoftDeletes trait, then it's a bit simpler:

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同