Laravel collection merge removes an item?

Posted 4 months ago by mikkelbfriis

Hiya, casts! I've recently been trying to figure out why my eloquent->merge removes an item?

$value['city'] = 'København';

$advertisements = App\Advertisement::whereHas('property', function ($q) use ($value) {
    return $q->where('city', 'LIKE', '%' . $value['city'] . '%');
})->take(4)->get();

$query = App\Property::where('city', 'LIKE', '%' . $value['city'] . '%')->whereDoesntHave('advertisement')->inRandomOrder()->take(4 - $advertisements->count());

$properties = $query->get();

$collection = $properties->merge($advertisements);

Sometimes it'll have the 4 it needs, however at some points it'll just return 3 items that's been merged. How can this be?

I've also tried dd'ing almost everything in the code:

dd([$collection, $properties, $advertisements, 4 - $advertisements->count(), $query]);

There's no duplicates, this I've tested severely to make sure there wasn't.

The merge is when there's not enough properties in the advertisements, so it's kind-of a fallback method.

Please sign in or create an account to participate in this conversation.