Support the ongoing development of Laravel.io →
Laravel Eloquent

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

]

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

David davidvera13 Joined 11 May 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.