1

I have a probleme with union() that not insert my new collection

During my each I have the $mean collection with witch I search the slices() I test and have some $_availabaleSlices to insert into my $slices (id: 4 then id:7) But after the whole code passed I only have the first $_availabaleSlices inside the $slices. What I have made wrong ?

The code

$slices = new Collection();

Means::whereIn('id', $meansId)
    ->get()
    ->each(function ($mean) use (&$slices, $weight) {
        $_availabaleSlices = $mean->slices()
            ->betweenWeight($weight)
            ->get();


        if (0 < $_availabaleSlices->count()) {
            dump('NEEDED',$_availabaleSlices->toArray());
            dump('BEFORE',$slices->toArray());
            $slices = $slices->union($_availabaleSlices);
            dump('AFTER',$slices->toArray());
        }
    });

dd('RESULTS', $slices->toArray());

The Results

"NEEDED"
array:1 [
  0 => array:5 [
    "id" => 4
    "deliveries_mean_id" => 3
    "weight_min" => 250.0
    "weight_max" => 600.0
    "deleted_at" => null
  ]
]
"BEFORE"
[]
"AFTER"
array:1 [
  0 => array:5 [
    "id" => 4
    "deliveries_mean_id" => 3
    "weight_min" => 250.0
    "weight_max" => 600.0
    "deleted_at" => null
  ]
]
"NEEDED"
array:1 [
  0 => array:5 [
    "id" => 7
    "deliveries_mean_id" => 4
    "weight_min" => 150.0
    "weight_max" => 700.0
    "deleted_at" => null
  ]
]
"BEFORE"
array:1 [
  0 => array:5 [
    "id" => 4
    "deliveries_mean_id" => 3
    "weight_min" => 250.0
    "weight_max" => 600.0
    "deleted_at" => null
  ]
]
"AFTER"
array:1 [
  0 => array:5 [
    "id" => 4
    "deliveries_mean_id" => 3
    "weight_min" => 250.0
    "weight_max" => 600.0
    "deleted_at" => null
  ]
]
"RESULTS"
array:1 [
  0 => array:5 [
    "id" => 4
    "deliveries_mean_id" => 3
    "weight_min" => 250.0
    "weight_max" => 600.0
    "deleted_at" => null
  ]
]

1 Answers1

1

Found the solution. I needed to use ->merge() and not ->union().

ouflak
  • 2,458
  • 10
  • 44
  • 49