lundi 5 avril 2021

Filter laravel collection in sub collection

My collection is like this.

Collection {
    0 => Name_Model {
        "id" => 44
        "name" => "The name of "
        "list_angg" => Collection {
            0 => Name_Model_sub {
                "code" => "02"
                "nameofcode" => "The name of 02"
            }
            1 => Name_Model_sub {
                "code" => "01"
                "nameofcode" => "The name of 01"
            }
        }
    }
    1 => Name_Model {
        "id" => 45
        "name" => "The name of thus"
        "list_angg" => Collection {
            0 => Name_Model_sub {
                "code" => "03"
                "nameofcode" => "The name of 3"
            }
        }
    }
}

I want to filter that model by value of list_angg->code. So I try like this. Filter and foreach that list_angg->code

$jurnals = $filterCollection->filter(function($value, $key) use ($kode_fakultas){
    foreach ($value->list_angg as $lists) {
        $filtered = $lists->where('code', $kode_fakultas);
        return $filtered;
    }
    return $filtered;
});
dd($jurnals);

I try use method reject() and map(). But filter didn't work as well. Is I miss something?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire