T
T
turindd2021-07-12 16:17:18
PHP
turindd, 2021-07-12 16:17:18

Laravel how to add 2 collections to one collection?

Hello.
How to add one collection to another?

$categories - a collection of all categories
$alias - an array of urls by which we are looking for categories
$array - all collections of categories should be saved here, but not in an array, otherwise it is difficult / inconvenient to get it later.

function searchCategoryAlias($categories, $alias, $array = null){

     foreach ($alias as $k => $v){

          $category = $categories->where('alias', $v);
          $array = collect($category);
     }
     return $array;
}


should be stored in this form (collections)
60ec40c36881e245684669.jpeg

and not in this form, i.e. if I declare an array $array[]
60ec40d635151419741251.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
turindd, 2021-07-14
@turindd

here even I sweated) $category returns an array that needs to be passed through foreach and added to $array in it

S
Sergey delphinpro, 2021-07-12
@delphinpro

You need to start by reading the documentation https://laravel.com/docs/8.x/collections
If then something is not clear, ask, showing what did not work out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question