我想一個數組轉換爲一個集合,映射了它。但是我收到一個錯誤: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