Laravel集合包含 [英] Laravel collection contains
本文介绍了Laravel集合包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在集合 https://laravel上使用Laravel contains
方法.com/docs/5.3/collections#method-contains .但这对我不起作用.
I'm using the Laravel contains
method on a collection https://laravel.com/docs/5.3/collections#method-contains. But it does not work for me.
foreach ($this->options as $option) {
if($options->contains($option->id)) {
dd('test');
}
}
dd($options);
看起来像这样:
Collection {#390
#items: array:1 [
0 => array:3 [
0 => array:7 [
"id" => 10
"slug" => "test"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
1 => array:7 [
"id" => 11
"slug" => "test-1"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
2 => array:7 [
"id" => 12
"slug" => "test-2"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
]
]
}
dd($option->id);
的结果是10
.
可能是什么问题?还是有更好的方法?
What could be wrong? Or is there a better way?
推荐答案
您应该将键/值对传递给contains方法,该方法将 确定给定对是否存在于集合中.
You should pass a key / value pair to the contains method, which will determine if the given pair exists in the collection.
您应该以这种方式使用 contains()
方法:
You should use contains()
method in this way:
foreach ($this->options as $option) {
// Pass key inside contains method
if($option->contains('id', $option->id)) {
dd('test');
}
}
希望这会有所帮助
这篇关于Laravel集合包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文