Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davidvera's avatar

[SOLVED] Data is duplicated in the view ...

I created 3 tables :

  • valuechains,
  • segments,
  • keyneeds.

A valuechain has many segments and segments has many keyneeds... For this I created the eloquent relationships in the concerned models.

I also created a method which displays informations :

public function getEntitiesWithKeyneeds()
{
    $valuechains = Valuechain::orderBy('valuechains.id')
        ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
        ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
        ->where('langs.isMainlanguage', '=', '1')
        ->with('segments')
        ->with('keyneeds')
        ->withCount('segments')
        ->withCount('keyneeds')
        ->get();


    foreach ($valuechains as $valuechain) {
        $ids[] = $valuechain->id;
    }
    $valuechainsCount = count($valuechains);

    foreach ($ids as $id) {
        $vcskns[] = Segment::select(
            'lang_valuechain.vcname', 'lang_valuechain.vcshortname',
            'lang_segment.segname', 'lang_segment.segshortname', 'segments.id',
            'lang_segment.created_at', 'lang_segment.updated_at', 'lang_segment.deleted_at'
        )
            ->distinct()
            ->withCount('keyneeds')
            ->join('lang_segment', 'segments.id', '=', 'lang_segment.segment_id')
            ->join('valuechains', 'segments.valuechain_id', '=', 'valuechains.id')
            ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
            ->join('sectors', 'valuechains.sector_id', '=', 'sectors.id')
            ->join('lang_sector', 'sectors.id', '=', 'lang_sector.sector_id')
            ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
            ->where([
                ['langs.isMainlanguage', '=', '1'],
                ['valuechains.id', '=', $id]
            ])
            ->whereNull('valuechains.deleted_at')
            ->whereNull('sectors.deleted_at')
            ->whereNull('segments.deleted_at')
            ->get();

    }
    return view('admin.tests', compact('vcskns', 'segmentsCount', 'valuechainsCount'));
}

In my view i want to display foreach valuechains the corresponding segments with the count of keyneeds...

In my view I have :

@for ($i=0; $i<$valuechainsCount; $i++)
    @foreach($vcskns[$i] as $vcskn[$i])
        {{  $vcskn[$i]->vcname }}<br>
    @endforeach
    @foreach($vcskns[$i] as $vcskn[$i])
        {{ $vcskn[$i]->segname }} - {{ $vcskn[$i]->keyneeds_count }}<br>
    @endforeach
@endfor

As a result I obtain this :

VC1
VC1
    SEGMENT1 - 0
    SEGMENT2 - 0
VC2
    SEGMENT3 - 0

I just wonder why VC1 appears twice... Thanks

0 likes
9 replies
Cronix's avatar

I would guess because you have this in a loop by itself

@foreach($vcskns[$i] as $vcskn[$i])
        {{  $vcskn[$i]->vcname }}<br>
    @endforeach

and VCS is the name twice.

Why aren't those loops combined since they are looping over the exact same thing?

@for ($i=0; $i<$valuechainsCount; $i++)
    @foreach($vcskns[$i] as $vcskn[$i])
        {{  $vcskn[$i]->vcname }}<br>
        {{ $vcskn[$i]->segname }} - {{ $vcskn[$i]->keyneeds_count }}<br>
    @endforeach
@endfor

I also don't understand what all of those indexes are for... doesn't this give you the same thing?

@foreach($vcskns as $vcskn)
        {{ $vcskn->vcname }}<br>
        {{ $vcskn->segname }} - {{ $vcskn->keyneeds_count }}<br>
@endforeach 
1 like
davidvera's avatar

To answer your comment :

@for ($i=0; $i<$valuechainsCount; $i++)
    @foreach($vcskns[$i] as $vcskn[$i])
        {{  $vcskn[$i]->vcname }}<br>
        {{ $vcskn[$i]->segname }} - {{ $vcskn[$i]->keyneeds_count }}<br>
    @endforeach
@endforeach 

gives as result :

VC 1
    Segment1
VC 1 
    Segment2
VC 2
Segment 3

This code :

@foreach($vcskns as $vcskn)
        {{ $vcskn->vcname }}<br>
        {{ $vcskn->segname }} - {{ $vcskn->keyneeds_count }}<br>
@endforeach

gives me error message :

Property [vcname] does not exist on this collection instance. (View: C:\wamp64\www\network-dev\resources\views\admin\tests.blade.php)

Vilfago's avatar

Could you dump($vcskns) to see how it lools like ?

davidvera's avatar

Of course ....

@for ($i=0; $i<$valuechainsCount; $i++)
    @foreach($vcskns[$i] as $vcskn[$i])
        {{  $vcskn[$i] }}
    @endforeach
@endfor

Gives me this result :

{"vcname":"VC1","vcshortname":"VC1","segname":"S1-1","segshortname":"S1-1","id":1, ... ,"keyneeds_count":0} 
{"vcname":"VC1","vcshortname":"VC1","segname":"S2-1","segshortname":"S1-2","id":2, ..., "keyneeds_count":0} 
{"vcname":"VC2","vcshortname":"VC2","segname":"S1-2","segshortname":"S2-1","id":3, ..., "keyneeds_count":0}
Snapey's avatar

Not sure what you did.... not what was asked.

davidvera's avatar

the dd($vcskns) generates an array of collections of arrays ...

array:2 [▼
  0 => Collection {#659 ▼
    #items: array:2 [▼
      0 => Segment {#660 ▼
        #fillable: array:9 [▶]
        #dates: array:3 [▶]
        #connection: "mysql"
        #table: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:9 [▼
          "vcname" => "VC1"
          "vcshortname" => "VC1"
          "segname" => "seg1"
          "segshortname" => "Seg1"
          "id" => 1
          "created_at" => "2018-06-07 09:18:31"
          "updated_at" => "2018-06-07 09:18:31"
          "deleted_at" => null
          "keyneeds_count" => 0
        ]
        #original: array:9 [▶]
        #changes: []
        #casts: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #guarded: array:1 [▶]
        #forceDeleting: false
      }
      1 => Segment {#661 ▼
        ...
        #attributes: array:9 [▼
          "vcname" => "VC1"
          "vcshortname" => "VC1"
          "segname" => "seg2"
          "segshortname" => "Seg2"
          "id" => 2
          "created_at" => "2018-06-08 08:02:41"
          "updated_at" => "2018-06-08 08:02:41"
          "deleted_at" => null
          "keyneeds_count" => 0
        ]
        ....
      }
    ]
  }
  1 => Collection {#717 ▶}
]

I want to obtains

VC1
    Seg 1 (keyneeds_count for seg1)
    Seg 2 (keyneeds_count for seg2)
VC2
    Seg 3 (keyneeds_count for seg3)
    Seg 4 (keyneeds_count for seg4)
36864's avatar
36864
Best Answer
Level 13

Quick and dirty fix:

@for ($i=0; $i<$valuechainsCount; $i++)
    {{  $vcskn[$i]->first()->vcname ?? "Empty Set"}}<br>
    @foreach($vcskns[$i] as $vcskn[$i])
        {{ $vcskn[$i]->segname }} - {{ $vcskn[$i]->keyneeds_count }}<br>
    @endforeach
@endfor
1 like
davidvera's avatar

Dirty but efficient ! I'll find another solution later when the application will be working... thanks !

Please or to participate in this conversation.