【发布时间】:2015-10-18 03:22:27
【问题描述】:
我目前正在开发一个 laravel 应用程序,我正在尝试从 eloquent 从数据库返回的对象创建一个数组。对象是这样的,
[
{
"id": 3,
"body": "Fake User created the project Fake Project 2",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "xxxxxxxxxx@gmail.com",
"first_name": "xxx1",
"last_name": "xxxx1",
"display_name": "xxxxxxxxxxx",
"initials": "XX",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"cost_visible": 0,
"login_type": "normal",
"api_token": null,
"created_at": "2015-02-13 11:47:24",
"updated_at": "2015-05-06 22:43:23",
"deleted_at": null,
"pivot": {
"notification_id": 3,
"user_id": 25,
"is_read": 0
}
}
],
"project": {
"id": 422,
"name": "Project Number 1",
"slug": "Tdc3de97",
"uri_hash": "project-number-1",
"description": null,
"total_cost": "1000.00",
"start_date": "2015-07-21",
"finish_date": "2015-10-22",
"status": 2,
"sales_person": null,
"client_id": 0,
"organisation_id": 97,
"owner_id": 97,
"user_id": 25,
"locked_by": null,
"created_at": "2015-07-21 13:39:21",
"updated_at": "2015-07-24 10:07:38",
"deleted_at": null,
"archived_at": "0000-00-00 00:00:00",
"invoiced_at": null,
"is_internal": 0
}
},
{
"id": 4,
"body": "Fake User created the project Fake Project 3",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "XXXXXX@gmail.com",
"first_name": "XXXXX",
"last_name": "XXXXXXXX",
"display_name": "XXX",
"initials": "XX",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"cost_visible": 0,
"login_type": "normal",
"api_token": null,
"created_at": "2015-02-13 11:47:24",
"updated_at": "2015-05-06 22:43:23",
"deleted_at": null,
"pivot": {
"notification_id": 4,
"user_id": 25,
"is_read": 0
}
}
],
"project": {
"id": 422,
"name": "Project Number 1",
"slug": "Tdc3de97",
"uri_hash": "project-number-1",
"description": null,
"total_cost": "1000.00",
"start_date": "2015-07-21",
"finish_date": "2015-10-22",
"status": 2,
"sales_person": null,
"client_id": 0,
"organisation_id": 97,
"owner_id": 97,
"user_id": 25,
"locked_by": null,
"created_at": "2015-07-21 13:39:21",
"updated_at": "2015-07-24 10:07:38",
"deleted_at": null,
"archived_at": "0000-00-00 00:00:00",
"invoiced_at": null,
"is_internal": 0
}
},
{
"id": 5,
"body": "Fake User created the project Fake Project 4",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "XXXXXXX@gmail.com",
"first_name": "XXXXXX",
"last_name": "XXXXXXX",
"display_name": "XXXXXXX",
"initials": "XX",
"remember_me": null,
"active": "1",
"invite_code": null,
"forgotten_code": null,
"cost_visible": 0,
"login_type": "normal",
"api_token": null,
"created_at": "2015-02-13 11:47:24",
"updated_at": "2015-05-06 22:43:23",
"deleted_at": null,
"pivot": {
"notification_id": 5,
"user_id": 25,
"is_read": 0
}
}
],
"project": {
"id": 422,
"name": "Project Number 1",
"slug": "Tdc3de97",
"uri_hash": "project-number-1",
"description": null,
"total_cost": "1000.00",
"start_date": "2015-07-21",
"finish_date": "2015-10-22",
"status": 2,
"sales_person": null,
"client_id": 0,
"organisation_id": 97,
"owner_id": 97,
"user_id": 25,
"locked_by": null,
"created_at": "2015-07-21 13:39:21",
"updated_at": "2015-07-24 10:07:38",
"deleted_at": null,
"archived_at": "0000-00-00 00:00:00",
"invoiced_at": null,
"is_internal": 0
}
}
}]
基本上,查询会返回所有带有未读通知的用户,我想将其与任何通知一起构建到数组中并发送电子邮件,但数组需要非常具体地包含它包含的详细信息。
我想把这样的东西发送到邮件功能来发送提醒,
'user@emailaddress.com' => array(
'first_name' => 'User',
'last_name' => 'Lastname',
'projects' => array(
'Project Title 1' => array(
'notifications' => array(
[0] => 'Notification 1',
[1] => 'Notification 2',
[2] => 'Notification 3'
)
)
)
)
我无法确定如何从我的对象构建这个数组,我目前在我的代码中有这个,
foreach($unread as $object) {
$emails_to_send = array(
'user' => $object->user[0],
'uri' => $object->uri,
'body' => $object->body,
'project' => $object->project
);
}
然而,这会构建错误的结构并覆盖最后一个循环。
【问题讨论】:
标签: php arrays object laravel eloquent