I created this method :
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();
}
$segmentCount = collect($segmentCount);
$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);
} }
My issue is that i have to match the each valuechains with the segment count ... On one hand I have a Collection on the other I have an array. I tried to convert this array onto collection but it didn't work...
If i return $segmentCount I obtain : [6,6,3,2] If i return $valuechainLists I obtain a collection but the $segmentCount are not matching (i should have "count" => 2 :
3 => Valuechain {#453 ▼
#table: "valuechains"
....
#attributes: array:4 [▼
"id" => 4
"vcname" => "test4"
"vcshortname" => "test4"
"count" => 6
]
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community