Home > Software engineering >  Unique collection Laravel
Unique collection Laravel

Time:04-22

I wonder how to distinct values in this Laravel collection because there is sub array of objects

There is too much sub-arrays, but i don't know how to filter this collection to get less sub-arrays

I wish to get less sub-arrays so i could make it unique and disctinct values within the collection

This is how i get this :

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {

            return (object) [
                $categorie->categorie->map(function ($items) {
                    
                    return $items;
                })
            ];
        });

This is the result !

 Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Fongicides et assimilés
                                        )
        
                                )
        
                        )
        
                    [1] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Autres
                                        )
        
                                )
        
                        )
        
                    [2] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Insecticides et assimilés
                                            [1] => Fongicides et assimilés
                                        )
        
                                )
        
                        )
        
                    [3] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Autres
                                        )
        
                                )
        
                        )
        
                )
        
        )

CodePudding user response:

Laravel collections have a lot of options

I think you're looking for flatten


Besides that you should change your code a little bit, the first return object [...] gives a weird result. I think you want the following:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {
    return $categorie->categorie->map(function ($items) {
        return $items;
    });
})->flatten();

Edit:

That can be shortened to:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {
    return $categorie->categorie;
})->flatten();
  • Related