I am working on a laravel application at the moment, and I am trying to create an array from an object that is returned from the database by eloquent. The object looks like this,
[
{
"id": 3,
"body": "Fake User created the project Fake Project 2",
"uri": "8jss90xk",
"object_id": 422,
"user": [
{
"id": 25,
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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
}
}
}]
Basically the query returns all the users with unread notifications, and I want to build that into array along with any notifications and send an email but the array needs to be quite specific with the details it contains.
I imagine sending something like this to mail function that will send the reminders,
'[email protected]' => array(
'first_name' => 'User',
'last_name' => 'Lastname',
'projects' => array(
'Project Title 1' => array(
'notifications' => array(
[0] => 'Notification 1',
[1] => 'Notification 2',
[2] => 'Notification 3'
)
)
)
)
I cannot for the live of me work out how to build this array from my object, I currently have this in my code,
foreach($unread as $object) {
$emails_to_send = array(
'user' => $object->user[0],
'uri' => $object->uri,
'body' => $object->body,
'project' => $object->project
);
}
This however builds the wrong structure and overwrites the last loop.