我试图从我的模型中收集数据。我遇到这个错误Undefined offset:1。我使用了Laravel 5.2
我一直在寻找解决方案但仍然没有得到它
$data = collect();
$leads = $school->students()->leads()->with('relationships');
foreach ($leads->get() as $key => $lead) {
$data->push([
'id' => $lead->id,
'name' => $lead->name,
'mobile_number' => $lead->mobile_number,
'email' => $lead->email,
'age' => $lead->age ? $lead->age : 'N/A',
'birthdate' => $lead->birthdate ? $lead->birthdate : 'N/A',
'guardian_name' => $lead->relationships[$key]->name,
]);
};
如果空键
,如何跳过$ key这是数组 1有关系,另一个是空的。如果关系或数据为空,如何进行键跳过
0 => array:7 [▼
"id" => 11654124
"name" => "Rhyl Meizhari"
"mobile_number" => "09491919882"
"email" => "rhyl@mei.zhari"
"age" => 6
"birthdate" => "03/01/2012"
"a" => Collection {#1463 ▼
#items: array:1 [▼
0 => Relationship {#1466 ▼
#historyCreationsEnabled: true
#dontKeepHistoryOf: array:1 [▶]
#appends: array:2 [▶]
#hidden: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:14 [▶]
#original: array:14 [▶]
#relations: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
-originalData: []
-updatedData: []
-updating: false
-dontKeep: []
-doKeep: []
#dirtyData: []
#affected_id: null
#message: []
-foreign_key: false
}
]
}
]
1 => array:7 [▼
"id" => 11658815
"name" => "Michelle Gabrillo"
"mobile_number" => "09060484380"
"email" => "mituyako10@gmail.com"
"age" => 24
"birthdate" => "01/02/1994"
"a" => Collection {#1448 ▼
#items: []
}
]
答案 0 :(得分:0)
我设法制作此'guardian_name' => $lead->relationships->first()
并返回此
0 => array:7 [▼
"id" => 11654124
"name" => "Rhyl Meizhari"
"mobile_number" => "09491919882"
"email" => "rhyl@mei.zhari"
"age" => 6
"birthdate" => "03/01/2012"
"guardian_name" => Collection {#1463 ▼
#items: array:1 [▶]
}
]
1 => array:7 [▼
"id" => 11658815
"name" => "Michelle Gabrillo"
"mobile_number" => "09060484380"
"email" => "mituyako10@gmail.com"
"age" => 24
"birthdate" => "01/02/1994"
"guardian_name" => null
]
问题是我不知道如何访问关系属性数组
0 => array:7 [▼
"id" => 11654124
"name" => "Rhyl Meizhari"
"mobile_number" => "09491919882"
"email" => "rhyl@mei.zhari"
"age" => 6
"birthdate" => "03/01/2012"
"guardian_name" => Collection {#1463 ▼
#items: array:1 [▼
0 => Relationship {#1466 ▼
#historyCreationsEnabled: true
#dontKeepHistoryOf: array:1 [▶]
#appends: array:2 [▶]
#hidden: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:14 [▼
"id" => 2970
"type" => "Payer"
"sub_type" => "Father"
"student_id" => 11654124
"first_name" => "Mityak"
"last_name" => "Badayos"
"email" => "michelle@themartialartsbusiness.software"
"contact_number" => "09491919882"
"access_permissions" => "null"
"existing_user" => 11650816
"ack" => 1
"ack_key" => "116e67c3ceaa8dcd9051e7c2a4aefbaa"
"created_at" => "2017-10-30 10:45:08"
"updated_at" => "2017-10-30 10:45:08"
]
#original: array:14 [▶]
#relations: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
-originalData: []
-updatedData: []
-updating: false
-dontKeep: []
-doKeep: []
#dirtyData: []
#affected_id: null
#message: []
-foreign_key: false
}
]
}
]
可以在不使用密钥的情况下手动访问#attributes