How does laravel create an empty set merge and merge it with a non-empty collection?

want to do a search function, use multiple keywords to search the same field of the same model (such as description), each keyword search out a collection, these sets may have duplicate content, my idea is to merge these sets first. Then remove the duplicate information.

$search_key_array = explode(" ", $keyword);
//
$collection = collect([]);
foreach ($search_key_array as $value) {

    $products = Product::where("description","like","%".$value."%")
        ->get();
    if ($products->isNotEmpty()) {
        $collection->concat($products);
    }
}
//
$collection = $collection->unique()->values();

since I need an initial empty collection, I create it with collect () and append the searched collection to the empty collection using the concat method found in the document. As a result, I find that with this concat () method, there is no change in $conllection. Ask for help from the boss to see what the problem is?

Feb.28,2021

look at the document carefully and use the collapse method

$collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

$collapsed = $collection->collapse();

$collapsed->all();

you can use splice:

$a = collect([]);
$a->splice(0, 0, [0 => ['k' => 'd'], 1 => ['k' => 's']]);
$a->splice(2, 0, [0 => ['k' => 'd2'], 1 => ['k' => 's2']]);
dd($a);
During the

loop, the first parameter needs to dynamically calculate the $a length.

Menu