laravel collection contains not working properly #33168
Replies: 4 comments
-
Moving to discussions. |
Beta Was this translation helpful? Give feedback.
-
Just used tinker to run your code. When you collapse that collection, 'color' => 'blue' is overwritten by 'color' => 'red', so it is correct: $col = collect([
['color' => 'blue', 4,2],
['x', 'y', 'color' => 'red', 'shape' => 'circle', 300],
['make' => 'Acura', 'type' => 'NSX']
]);
$col->contains('color', 'blue'); //true
$collapsed = $col->collapse();
dump($collapsed);
Illuminate\Support\Collection {#3075
all: [
"color" => "red",
0 => 4,
1 => 2,
2 => "x",
3 => "y",
"shape" => "circle",
4 => 300,
"make" => "Acura",
"type" => "NSX",
],
}
$collapsed->contains('color', 'blue'); //false |
Beta Was this translation helpful? Give feedback.
-
in above code, if you remove 'color => red' entry and use collapse still it returns false.
|
Beta Was this translation helpful? Give feedback.
-
Yes it looks like "contains" expects a collection of object-like elements, ie: $col = collect([
['name' => 'Bob', 'age' => 30, 'color' => 'blue'],
['name' => 'Anne', 'age' => 28, 'color' => 'red'],
['name' => 'Sally', 'age' => 29, 'color' => 'purple'],
]);
$col->contains('name', 'Anne'); //true
$col->contains('age', 41); //false With a simple flat array, If you dig into the source for |
Beta Was this translation helpful? Give feedback.
-
Description: laravel collection contains method not working properly
for example :
without collapse() contains work properly after collapse() and then use contains() it not working properly
it will return result as false but 'color' key with value 'blue' is there .
Steps To Reproduce: contains() should be working with collapse() method..
Beta Was this translation helpful? Give feedback.
All reactions