mercredi 11 juillet 2018

Flatten a Collection, with nested arrays and collections

I am having a ton of issues with this collection, I am trying to access the internals, i.e. id, name, desc,status but my code has gotten a little sloppy and now I cant seem to fix it.

Posted below is screenshots of what the nesting currently looks like:

enter image description here

I want to be able to do something like below in my view but cant seem to figure out how to properly flatten this nesting.

<div class="list-group">
    @foreach($apps as $app)
            <a href="/servers/" class="text-dark list-group-item list-group-item-" ></a>
    @endforeach
</div>

My current method which creates all of this madness:

private function createAppArray($apps, $uniqueApps){

    $final = collect([]);
    for($i = 0; $i < count($apps); $i++){
        foreach($uniqueApps as $uniqueApp){
            if($apps[$i]->id == $uniqueApp->aid){
                $test = collect($apps[$i]);
                $final->push($test->merge(['status' => 'warning']));
                $i++;
            } 
        }
    }
        dd($final);
    return $final;
}

Again, I am trying to be able to do something like the blade code above within my view. I have tried to do nested for-each loops within my blade and also tried just flattening the entire collection with no luck.

Does anyone have any idea how I can accomplish the desired results? Thank you in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire