我想一个数组转换为一个集合,映射了它。但是我收到一个错误:Laravel收集工作不
ErrorException in Poll.php line 90:
Undefined index: id
代码:
$options = collect($options);
$options->map(function($option) {
if($this->options->contains('id', $option['id'])) { //line 90
Option::create($option);
}
});
当我dd($options);
它看起来像这样:
Collection {#390
#items: array:4 [
0 => array:7 [
"id" => 17
"slug" => "asdf-1"
"name" => "asdf"
"poll_id" => 6
"created_at" => "2016-11-13 17:39:01"
"updated_at" => "2016-11-13 17:39:01"
"votes" => []
]
1 => array:7 [
"id" => 18
"slug" => "asdfkowugiw"
"name" => "asdfkowugiw"
"poll_id" => 6
"created_at" => "2016-11-13 17:39:01"
"updated_at" => "2016-11-13 17:39:01"
"votes" => []
]
2 => array:7 [
"id" => 21
"slug" => "asdf-1"
"name" => "asdf"
"poll_id" => 6
"created_at" => "2016-11-13 17:48:37"
"updated_at" => "2016-11-13 17:48:37"
"votes" => []
]
3 => array:7 [
"id" => 22
"slug" => "asdfkowugiw"
"name" => "asdfkowugiw"
"poll_id" => 6
"created_at" => "2016-11-13 17:48:37"
"updated_at" => "2016-11-13 17:48:37"
"votes" => []
]
]
}
所以这是一个集合,所以我应该能够做到这一点对不对?:
$options = collect($options);
$options->map(function($option) {
if($this->options->contains('id', $option->id)) {
Option::create($option);
}
});
但这失败了。这里有什么问题?
- 编辑 -
array:7 [
"id" => 17
"slug" => "asdf-1"
"name" => "asdf"
"poll_id" => 6
"created_at" => "2016-11-13 17:39:01"
"updated_at" => "2016-11-13 17:39:01"
"votes" => []
]
这是我收到的问题:'''试图让非object''' – Jamie
财产尝试打印$选项里面什么? –
请参阅我的编辑。 – Jamie