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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧