Php 拉维收藏包含
我正在集合上使用Laravel contains方法。但这对我不起作用Php 拉维收藏包含,php,laravel,collections,Php,Laravel,Collections,我正在集合上使用Laravel contains方法。但这对我不起作用 foreach ($this->options as $option) { if($options->contains($option->id)) { dd('test'); } } dd$期权;看起来像这样: Collection {#390 #items: array:1 [ 0 => array:3 [ 0 => array:7 [
foreach ($this->options as $option) {
if($options->contains($option->id)) {
dd('test');
}
}
dd$期权;看起来像这样:
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$选项->id的结果;是10岁
有什么不对劲吗?还是有更好的办法
您应该将一个键/值对传递给contains方法,该方法将
确定集合中是否存在给定对
您应该以以下方式使用方法:
foreach ($this->options as $option) {
// Pass key inside contains method
if($option->contains('id', $option->id)) {
dd('test');
}
}
希望这有助于使用以下命令,告诉Laravel您希望匹配“id”:
foreach ($this->options as $option) {
if(!$options->flatten(1)->where('id',$option->id)->isEmpty()) {
dd('test');
}
}
$options->contains('id', $option->id);